【问题标题】:C# WebClient Https GET request 3x slower than other sitesC# WebClient Https GET 请求比其他站点慢 3 倍
【发布时间】:2019-12-10 00:34:29
【问题描述】:

我不能在这个项目中使用 google api,但需要做一个简单的 google 查询,我通过在 WebClient 上使用 ssl3 和 tls12 来做到这一点,手动设置标题(不确定这是否有帮助)并简单地发送一个 GET 请求,由于某种原因,这需要 10 秒,但 StackOverflow 只需 3 秒。然而,当使用 chrome 都立即加载时,使用 WebClient 的瓶颈是什么?如何像 chrome 一样快速获取 SSL GET 请求?

第二个问题:如果页面包含 JS,如何在不使用网络浏览器渲染整个内容的情况下在检索到的“文档”上执行 js p>

任何帮助表示赞赏。

编辑:删除标题修改代码会加快速度,但谷歌仍然非常慢,我假设他们是故意这样做的?有没有办法解决这个问题?

//in main
  WebCrawler wc = new WebCrawler();
            string page = wc.load("https://stackoverflow.com/questions/20064505/requesting-html-over-https-with-c-sharp-webclient");
            page = wc.load("https://www.google.com/maps?q=computer+shops+near+me&rlz=1C1GCEA_enZA855ZA855&um=1&ie=UTF-8&sa=X&ved=0ahUKEwi1lY-c4eDjAhUtWhUIHf8DDKUQ_AUIEigB");

...
// webcrawler class
WebClient webClient;
        public WebCrawler()
        {

            webClient = new WebClient();
            ServicePointManager.ServerCertificateValidationCallback += ValidateRemoteCertificate;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
            ServicePointManager.Expect100Continue = true;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;



        }
        public  string load(string uri)
        {
            Uri address = new Uri(uri);

            {
                webClient.Headers.Set(HttpRequestHeader.UserAgent, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36");
                webClient.Headers.Set(HttpRequestHeader.Referer, "https://www.google.com/");
             //    webClient.Headers.Set(HttpRequestHeader.Cookie,
                var stream = webClient.OpenRead(address);
                using (StreamReader sr = new StreamReader(stream))
                {
                    var page = sr.ReadToEnd();
                    return page;
                }
            }
        }
        private static bool ValidateRemoteCertificate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error)
        {
            if (error == System.Net.Security.SslPolicyErrors.None)
            {
                return true;
            }

            Console.WriteLine("X509Certificate [{0}] Policy Error: '{1}'",
                cert.Subject,
                error.ToString());

            return false;
        }
    }

【问题讨论】:

    标签: c# google-maps https webclient


    【解决方案1】:

    不要使用 WebClient。相反,您可以使用HttpClientHttpWebRequest 并将AutomaticDecompression 设置为GZip, Deflate

    当您使用以下行将AutomaticDecompression 设置为GZip, deflate 时(例如,reqHttpWebRequest):

    req.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
    

    一个名为Accept-Encoding 的HTTP 标头将被发送到服务器,其值为GZip, Deflate,要求服务器以压缩格式下载内容。 这意味着,要下载的内容更小,所需的时间也更短。 HttpWebRequest 将负责解压缩从服务器发送的数据。

    我在HttpWebRequest 上解释的概念可以应用于HttpClient

    【讨论】:

    • 这个支持https吗?
    • @BinkyNichols,是的。您可以使用 HttpWebRequestHttpClient 并将 AutomaticDecompression 设置为 https 请求。
    • 根本不会缩短时间,问题是,当我在 WebBrowser 控件中加载同一页面时,大约需要 1 秒,而在纯 GET 请求中需要 10 秒。
    • 你能加快速度吗?我实现了以下两个设置:ServicePointManager.Expect100Continue = true;ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;,我的网络客户端速度从原来的水平下降了几秒钟。但是我必须声明 TLS1.2 否则它会尝试使用其他东西并且网络请求会失败。
    猜你喜欢
    • 2014-06-04
    • 2018-03-25
    • 2012-10-31
    • 2013-12-02
    • 2015-07-18
    • 2022-10-06
    • 1970-01-01
    • 2013-04-23
    相关资源
    最近更新 更多