Search code examples
vb.nethttpwebrequest

VB.net HttpWebRequest returns 403 forbidden when ressource is available


hello i was using the code for a while in my vb.net app but today when i tried it it returned a 403 error.

Shared Function GetHtmlPage(ByVal http://mmo-stream.net/AiaSpecto/tester.php As String) As String

    Dim strResult As String
    Dim objResponse As WebResponse
    Dim objRequest As WebRequest

    objRequest = HttpWebRequest.Create(strURL)

    objResponse = objRequest.GetResponse()
    Using sr As New StreamReader(objResponse.GetResponseStream())
        strResult = sr.ReadToEnd()
        sr.Close()
    End Using
    Return strResult

End Function

I really cant understand why i get a 403 error because when i go on the link itself i get the OK message.


Solution

  • The request wants a useragent header. If you change the object to HttpWebResponse and HttpWebRequest and add a generic useragent it should return OK :

    Dim objResponse As HttpWebResponse
    Dim objRequest As HttpWebRequest
    objRequest = HttpWebRequest.Create(strURL)
    objRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36"
    objResponse = objRequest.GetResponse()