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

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

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

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

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

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

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

之前的写法:

</>code

  1. ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3

能解决问题的写法:

</>code

  1. 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  对象之前,在创建一个代理服务器的对象,并且把服务器的代理地址和端口实例化到代理服务器对象。


</>code

  1. public  string GetHtml(string url, byte[] postData, bool isPost, CookieContainer cookieContainer,string refurl)
  2.         {
  3.             ServicePointManager.Expect100Continue = false;
  4.             Thread.Sleep(NetworkDelay);//等待
  5.             currentTry++;
  6.             HttpWebRequest httpWebRequest = null;
  7.            
  8.             HttpWebResponse httpWebResponse = null;
  9.             try
  10.             {
  11.                // byte[] byteRequest = Encoding.Default.GetBytes(postData);
  12.                 byte[] byteRequest = postData;
  13.                 httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
  14.                 if (Proxy != null)
  15.                     httpWebRequest.Proxy = Proxy;      //代理服务器设置
  16.                 httpWebRequest.CookieContainer = cookieContainer;
  17.                 httpWebRequest.ContentType = contentType;
  18.                 httpWebRequest.ServicePoint.ConnectionLimit = maxTry;
  19.                 httpWebRequest.Referer = refurl;
  20.                 httpWebRequest.Accept = accept;
  21.                 httpWebRequest.UserAgent = userAgent;
  22.                 httpWebRequest.Method = isPost ? "POST" : "GET";
  23.                 httpWebRequest.ContentLength = byteRequest.Length;
  24.                 Stream stream = httpWebRequest.GetRequestStream();
  25.                 stream.Write(byteRequest, 0, byteRequest.Length);
  26.                 stream.Close();
  27.                 httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
  28.                 Stream responseStream = httpWebResponse.GetResponseStream();
  29.                 StreamReader streamReader = new StreamReader(responseStream, encoding);
  30.                 string html = streamReader.ReadToEnd();
  31.                 streamReader.Close();
  32.                 responseStream.Close();
  33.                 currentTry = 0;
  34.                 httpWebRequest.Abort();
  35.                 httpWebResponse.Close();
  36.                 foreach (Cookie cookie in httpWebResponse.Cookies)
  37.                 {
  38.                     cookieContainer.Add(cookie);
  39.                 }
  40.                 return html;
  41.             }
  42.             catch (Exception)
  43.             {
  44.                 if (httpWebRequest != null)
  45.                 {
  46.                     httpWebRequest.Abort();
  47.                 } if (httpWebResponse != null)
  48.                 {
  49.                     httpWebResponse.Close();
  50.                 }
  51.                 return "";
  52.             }
  53.         }


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

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

不错 推荐

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

快速入口

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

其它栏目

· 建站教程
· 365学习

业务咨询

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

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

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