0

我试图获取 www.digikey.com 下页面的网页源它很久以前工作正常,但现在网站拒绝请求,但它在网络浏览器中显示没有问题,尝试通过代码访问时发生问题,当我检查现在收到的页面来源

您的请求有问题。我们无法处理您的请求。如果您认为自己错误地收到了此消息,请返回上一页重试或联系 Digi-Key 网站管理员。请参考以下事件编号,以便我们帮助您解决此错误。

我使用的代码是:

    Dim PartURL As String
    PartURL = "http://www.digikey.com"
    Dim request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create(PartURL)
    Dim response As System.Net.HttpWebResponse = request.GetResponse()
    Dim sr As System.IO.StreamReader = New System.IO.StreamReader(response.GetResponseStream())
    Dim sourcecode As String = sr.ReadToEnd()

当我将 url 更改为 google.com 时,它运行良好。www.digikey.com 网站在网络浏览器中正常运行。仅在尝试通过代码访问时才显示错误。所以我认为它可能与代码有关。是不是因为digikey拒绝了请求。有什么方法可以获取 www.digikey.com 下页面的源代码

4

1 回答 1

2

尝试这个:

Dim PartURL As String
PartURL = "http://www.digikey.com"
Dim request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create(PartURL)
request.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:2.0b8) Gecko/20100101 Firefox/4.0b8"
request.Accept = "Accept: text/html,application/xhtml+xml,application/xml"

Dim response As System.Net.HttpWebResponse = request.GetResponse()
Dim sr As System.IO.StreamReader = New System.IO.StreamReader(response.GetResponseStream())
Dim sourcecode As String = sr.ReadToEnd()
于 2013-09-26T11:28:04.390 回答