无法使用WebClient或HttpWebRequest加载页面

时间:2017-02-20 22:56:11

标签: vb.net webclient

无论我使用WebClient还是HttpWebRequest,加载此页面都会超时。我究竟做错了什么?它不能https,因为其他https网站加载得很好。

以下是我的最新尝试,它添加了我在Firefox的检查器中看到的所有标题。

一个有趣的行为是我无法用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

1 个答案:

答案 0 :(得分:0)

所以这最终归结于该项目仍然在.NET 3.5中。 .NET试图使用SSL加载网站https。添加此行可解决问题:

    ServicePointManager.SecurityProtocol = 3072

我必须使用3072,因为3.5不包含SecurityProtocolType.Tls12的定义。