Wednesday, February 16, 2011

VB.NET Webrequest with useragent

 Private Function readHtmlPage(ByVal url As String) As String
        Try
            Dim result As String
            Dim objResponse As WebResponse
            Dim objRequest As WebRequest = System.Net.HttpWebRequest.Create(url)
            DirectCast(objRequest, System.Net.HttpWebRequest).UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.38) Gecko/20101203 Firefox/3.6.13 (.NET CLR 3.5.26529)"

            objResponse = objRequest.GetResponse()
            Using sr As New StreamReader(objResponse.GetResponseStream())
                result = sr.ReadToEnd()
                'Close and clean up the StreamReader
                sr.Close()
            End Using
            Return result
        Catch ex As Exception
            'lblStatus.Text = ex.Message
            Return ex.Message.ToString
            Exit Function
        End Try
    End Function

No comments:

Post a Comment