vbnet Get HTML title

Private Function GetTitle() As String
	'retrieves the <title> from a given Url
	Dim uri As New Uri(UrlIn)
	Dim request As HttpWebRequest
	Dim response As HttpWebResponse
	Dim Title As String = "" 'returned <title>
	Dim HTML As String = "" 'html returned from web request
	Dim TempArray As Array 'array used to strip out the <title>
	Dim Temp As String = ""
	Dim DelimiterA() As String = {"<title>"}
	Dim DelimiterB() As String = {"</title>"}

	Try
		If (uri.Scheme = uri.UriSchemeHttp) Then
			request = HttpWebRequest.Create(uri)
			request.Method = WebRequestMethods.Http.Get
			request.Timeout = 60000 '1000ms = 1 second
			response = request.GetResponse
			Dim reader As New StreamReader(response.GetResponseStream())
			HTML = reader.ReadToEnd
			response.Close()
		Else
			HTML = ""
		End If

		If HTML <> "" Then
			If (InStr(HTML, "<title>") > 0) And (InStr(HTML, "</title>") > 0) Then
				TempArray = HTML.Split(DelimiterA, StringSplitOptions.None)
				Temp = TempArray(1)
				TempArray = Temp.Split(DelimiterB, StringSplitOptions.None)
				Title = TempArray(0)
			End If
		End If
	Catch ex As Exception
		Title = ""
	End Try

	Return Title
End Function
Gets the <title> element from another website - as used on slck.it

Updated: Tuesday 12th October 2010, 22:15am

There are 0 comments

Leave a comment of your own

Comments are currently closed.