您现在的位置: 365建站网 > 365文章 > HttpWebRequest 基础连接已经关闭: 接收时发生错误的解决方法

HttpWebRequest 基础连接已经关闭: 接收时发生错误的解决方法

文章来源:365jz.com     点击数:8946    更新时间:2020-06-27 22:55   参与评论

HttpWebRequest 基础连接已经关闭: 接收时发生错误 

HttpWebRequest 基础连接已经关闭: 发送时发生错误 

HttpWebRequest 基础连接已关闭 连接意外关闭

报的错误为:1. "基础连接已经关闭: 发送时发生错误";

之前的写法:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3

能解决问题的写法:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

分析:因为请求的url是基于https的,所以Post请求时必须添加ServicePointManager.SecurityProtocol。但选择哪个协议呢?一开始看到项目里面都是基于ssl的,索性也用了ssl,但是没有效果,最后干脆直接把所以的协议枚举用或的形式全都写出来,成功了。

SSL验证一定要写在WebRequest.Create(url)前面


针对使用httpWebRequest优化的几种解决方案:


      1、在GetResponse() 前加上这句ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;  这句代码是让你的程序适应https请求协议。


      2、httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);  在行代码前添加System.GC.Collect();   用于程序垃圾资源回收


      3、如果Http的请求,是设置了KeepAlive=true的话,那么对应的http的connection会和服务器保持连接的。所以如果上述办法都不能解决超时的问题,可以尝试将keepAlive设置为false试试,看看能否解决。


      4、httpWebRequest.ServicePoint.ConnectionLimit = maxTry;  默认ConnectionLimit是2个连接数,所以可以通过修改连接数尝试解决该问题。可以改到200-300,但是不要改太大,容易对程序照成压力。


      5、另外你初始化的都要在用完之后,进行关闭和回收。(HttpWebRequest  HttpWebResponse)     虽然每种开发语言都有自己的回收机制,但是要记着一点再好的人,也有累的时候,累了就不给你干活了,所以平时对它们好点。


      如果上面的方法都无法解决你的问题,可以尝试一下我最后的解决方案。


      最后我发现只有在一台服务器上面出问题,后来经过查找,发现是这台服务器被配置了代理服务器,通过代理服务器的方式进行外网的访问。所以找到原因就好办了,只要在创建HttpWebRequest  对象之前,在创建一个代理服务器的对象,并且把服务器的代理地址和端口实例化到代理服务器对象。


public  string GetHtml(string url, byte[] postData, bool isPost, CookieContainer cookieContainer,string refurl)
        {
            ServicePointManager.Expect100Continue = false;
            Thread.Sleep(NetworkDelay);//等待
            currentTry++;
            HttpWebRequest httpWebRequest = null;
           
            HttpWebResponse httpWebResponse = null;
            try
            {
               // byte[] byteRequest = Encoding.Default.GetBytes(postData);
                byte[] byteRequest = postData;
                httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
                if (Proxy != null)
                    httpWebRequest.Proxy = Proxy;      //代理服务器设置
                httpWebRequest.CookieContainer = cookieContainer;
                httpWebRequest.ContentType = contentType;
                httpWebRequest.ServicePoint.ConnectionLimit = maxTry;
                httpWebRequest.Referer = refurl;
                httpWebRequest.Accept = accept;
                httpWebRequest.UserAgent = userAgent;
                httpWebRequest.Method = isPost ? "POST" : "GET";
                httpWebRequest.ContentLength = byteRequest.Length;
                Stream stream = httpWebRequest.GetRequestStream();
                stream.Write(byteRequest, 0, byteRequest.Length);
                stream.Close();
                httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                Stream responseStream = httpWebResponse.GetResponseStream();
                StreamReader streamReader = new StreamReader(responseStream, encoding);
                string html = streamReader.ReadToEnd();
                streamReader.Close();
                responseStream.Close();
                currentTry = 0;
                httpWebRequest.Abort();
                httpWebResponse.Close();
                foreach (Cookie cookie in httpWebResponse.Cookies)
                {
                    cookieContainer.Add(cookie);
                }
                return html;
            }
            catch (Exception)
            {
                if (httpWebRequest != null)
                {
                    httpWebRequest.Abort();
                } if (httpWebResponse != null)
                {
                    httpWebResponse.Close();
                }
                return "";
            }
        }


如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛

发表评论 (8946人查看0条评论)
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
昵称:
最新评论
2022年01月24日 11:24apollo

不错 推荐

------分隔线----------------------------

快速入口

· 365软件
· 杰创官网
· 建站工具
· 网站大全

其它栏目

· 建站教程
· 365学习

业务咨询

· 技术支持
· 服务时间:9:00-18:00
365建站网二维码

Powered by 365建站网 RSS地图 HTML地图

copyright © 2013-2024 版权所有 鄂ICP备17013400号