Crimson-Med Crimson-Med - 1 year ago 72 Question HttpWebRequest returns 403 forbidden when ressource is available

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

Shared Function GetHtmlPage(ByVal 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()
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.

Answer Source

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()