关于HttpWebRequest调用远程HTTPS速度慢的问题

关于HttpWebRequest调用远程HTTPS速度慢的问题,C# Win平台

随着HTTP逐步被HTTPS取代,越来越多的远程接口调用也不得不从HTTP协议更换至HTTPS,笔者近期在维护一个老项目时,也遇到这个问题,就是HttpWebRequest调用远程HTTPS速度慢,经过查阅资料,初步判断问题出在WEB代理环节,通过设置以下两个地方(C# & Windows环境):

一、程序中设置代理为NULL

            HttpWebRequest request = null;
            if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
            {
                request = WebRequest.Create(url) as HttpWebRequest;
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                request.ProtocolVersion = HttpVersion.Version11;
                // 这里设置了协议类型。
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                request.KeepAlive = false;
                ServicePointManager.CheckCertificateRevocationList = true;
                ServicePointManager.DefaultConnectionLimit = 100;
                ServicePointManager.Expect100Continue = false;
            }
            else
            {
                request = (HttpWebRequest)WebRequest.Create(url);
            }

            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.Referer = null;
            request.AllowAutoRedirect = true;
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
            request.Accept = "*/*";
            request.Proxy = null;

request.Proxy = null; 仅显示部分代码片段,设置Proxy为null

二、由于Windows环境下,HttpWebRequest查找web 代理是通过IE来完成的,所以还得设置下IE

IE->Internet选项->连接->局域网设置->自动配置->自动检测设置(将此处设为“未勾选”)

 

经测试,问题解决。如有错误及描述不当之处,敬请指正!

posted on 2018-12-17 15:05  冒得味口  阅读(1238)  评论(0编辑  收藏  举报