加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 无法从传输连接读取数据:控制台应用程序中的连接已关闭错

发布时间:2020-12-15 04:22:05 所属栏目:百科 来源:网络整理
导读:我在控制台应用程序中有这个代码,它运行在一个循环中 try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(search); request.Headers.Add("Accept-Language","de-DE"); request.Method = "GET"; request.Accept = "text/html"; using (HttpWe
我在控制台应用程序中有这个代码,它运行在一个循环中
try
 {
      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(search);
      request.Headers.Add("Accept-Language","de-DE");
      request.Method = "GET";
      request.Accept = "text/html";
      using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
      {
           using (StreamReader reader = new StreamReader(response.GetResponseStream(),Encoding.ASCII))
           {
                string html = reader.ReadToEnd();
                    FindForMatch(html,url);
           }
      }
 }
 catch (Exception ex)
 {
      throw new Exception(ex.Message);
 }

经过几个循环

Unable to read data from the transport connection: The connection was closed

错误.任何想法为什么会这样?感谢名单..

解决方法

加入后
request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;

它工作正常..

我发现它形成这个博文

WebRequest and Unable to read data from the transport connection Error

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读