如果我直接在浏览器中输入URL,它会返回数据。 我正在重用我为其他网站编写的VB.net程序,但现在我得到了:
访问被拒绝:来自
的错误3 HTTP标头
Dim WebReq As HttpWebRequest = CType(WebRequest.Create(strURL), HttpWebRequest)
'If required by the server, set the credentials.
WebReq.Credentials = CredentialCache.DefaultCredentials
'Get the Respponse.
Dim WebResp As HttpWebResponse = CType(WebReq.GetResponse(), HttpWebResponse)
'Display the status.
Console.WriteLine(WebResp.StatusDescription)
' Open the stream using a StreamReader for easy access.
Dim ALEKStream As Stream = WebResp.GetResponseStream()
' Open the stream using a StreamReader for easy access.
Dim srRead As New StreamReader(ALEKStream)
' Read the content.
Dim responseFromServer As String = srRead.ReadToEnd()
TextBox1.Text = responseFromServer
答案 0 :(得分:1)
您可能需要为要连接的给定网站设置用户代理属性。如果没有用户代理,某些服务器将设置为拒绝请求。
WebReq.UserAgent = "TestUserAgent"
您甚至可能需要伪造使用者,使其看起来像浏览器。
WebReq.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"
资源:https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.useragent(v=vs.110).aspx
您可能还想通过网络浏览器突破提琴手并访问网站,然后比较浏览器浏览器与应用程序调用时的情况之间的差异。