Vb.net 无法使用WebClient或HttpWebRequest加载页面
无论我使用的是Vb.net 无法使用WebClient或HttpWebRequest加载页面,vb.net,webclient,Vb.net,Webclient,无论我使用的是WebClient还是HttpWebRequest,加载此页面都会超时。我做错了什么?它不可能是https,因为其他https站点加载得很好 下面是我的最新尝试,它添加了我在Firefox的inspector中看到的所有标题 一个有趣的行为是,我不能用Fiddler监控这一点,因为当Fiddler运行时,一切都正常工作 Using client As WebClient = New WebClient() client.Headers(HttpRequest
WebClient
还是HttpWebRequest
,加载此页面都会超时。我做错了什么?它不可能是https
,因为其他https
站点加载得很好
下面是我的最新尝试,它添加了我在Firefox的inspector中看到的所有标题
一个有趣的行为是,我不能用Fiddler监控这一点,因为当Fiddler运行时,一切都正常工作
Using client As WebClient = New WebClient()
client.Headers(HttpRequestHeader.Accept) = "text/html, image/png, image/jpeg, image/gif, */*;q=0.1"
client.Headers(HttpRequestHeader.UserAgent) = "Mozilla/5.0 (Windows; U; Windows NT 6.1; de; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12"
client.Headers(HttpRequestHeader.AcceptLanguage) = "en-US;en;q=0.5"
client.Headers(HttpRequestHeader.AcceptEncoding) = "gzip, deflate, br"
client.Headers(HttpRequestHeader.Referer) = "http://www.torontohydro.com/sites/electricsystem/Pages/foryourhome.aspx"
client.Headers("DNT") = "1"
client.Headers(HttpRequestHeader.KeepAlive) = "keep-alive"
client.Headers(HttpRequestHeader.Upgrade) = "1"
client.Headers(HttpRequestHeader.CacheControl) = "max-age=0"
Dim x = New Uri("https://css.torontohydro.com/")
Dim data as string = client.DownloadString(x)
End Using
所有这些都是多余的代码。把它煮成几行,会导致相同的挂起
Using client as WebClient = New WebClient()
Dim data as string = client.DownloadString("https://css.torontohydro.com")
End Using
简而言之,这就是HttpWebRequest
代码,它也挂起获取响应
Dim getRequest As HttpWebRequest = CreateWebRequest("https://css.torontohydro.com/")
getRequest.CachePolicy = New Cache.RequestCachePolicy(Cache.RequestCacheLevel.BypassCache)
Using webResponse As HttpWebResponse = CType(getRequest.GetResponse(), HttpWebResponse)
'no need for any more code, since the above line is where things hang
因此,这最终是由于该项目仍在.NET3.5中。NET正在尝试使用SSL加载该站点,该站点为
https
。添加此行修复了以下问题:
ServicePointManager.SecurityProtocol = 3072
我不得不使用
3072
,因为3.5不包含SecurityProtocolType.Tls12
的定义,所以这是由于该项目仍在.NET 3.5中。NET正在尝试使用SSL加载该站点,该站点为https
。添加此行修复了以下问题:
ServicePointManager.SecurityProtocol = 3072
我不得不使用
3072
,因为3.5不包含SecurityProtocolType.Tls12
的定义。为什么要为VB.Net代码道歉?你只是想要html页面吗?对不起,我想开个坏玩笑。我有几页要从网站上阅读,但第一页都读不完。似乎是特定于该站点的。无论是设置所有这些标题还是只使用最后两行,我都会得到相同的超时。或者,如果我使用HttpWebRequest
和GET
方法,我会在Firefox中用RESTClient插件加载它。你只是得到一个超时,没有其他特定的错误代码吗?你有没有试过把这个放到try。。catch..
block,并记录错误?这是常规try/catch
块中的超时异常。我无法获得任何.NET方法来加载页面。为什么要为VB.NET代码道歉?你只是想要html页面吗?抱歉,我想说一个不好的笑话。我有几页要从网站上阅读,但第一页都读不完。似乎是特定于该站点的。无论是设置所有这些标题还是只使用最后两行,我都会得到相同的超时。或者,如果我使用HttpWebRequest
和GET
方法,我会在Firefox中用RESTClient插件加载它。你只是得到一个超时,没有其他特定的错误代码吗?你有没有试过把这个放到try。。catch..
block,并记录错误?这是常规try/catch
块中的超时异常。我无法获取任何.NET方法来加载页面。