C#WebClient Https获取请求的速度比其他站点慢3倍

C#WebClient Https获取请求的速度比其他站点慢3倍,c#,google-maps,https,webclient,C#,Google Maps,Https,Webclient,我不能在这个项目中使用GoogleAPI,但需要做一个简单的google查询,我在WebClient上使用ssl3和tls12,手动设置标题(我不确定这是否有用),然后简单地发送一个GET请求,出于某些原因,这需要10秒,但一个StackOverflow只需要3秒。然而,当使用chrome时,使用WebClient的瓶颈是什么?我怎样才能像chrome一样快速获取SSL get请求 第二个问题:如果页面包含JS,如何在检索到的“文档”上执行JS,而不使用web浏览器并呈现整个内容 谢谢你的帮助

我不能在这个项目中使用GoogleAPI,但需要做一个简单的google查询,我在WebClient上使用ssl3和tls12,手动设置标题(我不确定这是否有用),然后简单地发送一个GET请求,出于某些原因,这需要10秒,但一个StackOverflow只需要3秒。然而,当使用chrome时,使用WebClient的瓶颈是什么?我怎样才能像chrome一样快速获取SSL get请求

第二个问题:如果页面包含JS,如何在检索到的“文档”上执行JS,而不使用web浏览器并呈现整个内容

谢谢你的帮助

编辑:删除标题修改代码会加快速度,但谷歌仍然非常慢,我想他们是故意这么做的吧?这有什么办法吗

//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;
        }
    }

不要使用WebClient。相反,您可以使用
HttpClient
HttpWebRequest
并将
自动压缩设置为
GZip,放气

当您将
AutomaticDecompression
设置为
GZip时,使用以下行(例如
req
HttpWebRequest
)进行放气:

req.AutomaticDecompression=DecompressionMethods.GZip | DecompressionMethods.Deflate;
一个名为
Accept Encoding
的HTTP头将发送到服务器,其值为
GZip,Deflate
,要求服务器下载压缩格式的内容。 这意味着要下载的内容越小,所需时间也越短。
HttpWebRequest
将负责解压缩从服务器发送的数据


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

这是否支持https?@BinkyNichols,是的。您可以使用
HttpWebRequest
HttpClient
设置https请求的
AutomaticDecompression
。这根本不会缩短时间,问题是,当我在WebBrowser控件中加载同一页面时,大约需要1秒,而纯GET请求只需要10秒。您能够加快速度吗?我实现了以下两个设置:
ServicePointManager.Expect100Continue=true
ServicePointManager.SecurityProtocol=SecurityProtocolType.Tls12和我的网络客户端速度从原来的速度下降了几秒钟。但是我必须声明TLS1.2,否则它会尝试使用其他东西,web请求会失败。