你好我在我的vb.net应用程序中使用了一段时间,但今天当我尝试它时它返回了403错误。
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
我真的不明白为什么我会收到403错误,因为当我进入链接本身时,我收到了OK消息。
答案 0 :(得分:3)
请求需要一个useragent标头。
如果您将对象更改为HttpWebResponse
和HttpWebRequest
并添加通用的useragent,则应该返回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()