【问题标题】:C# : System.Net.WebException: The underlying connection was closedC#:System.Net.WebException:底层连接已关闭
【发布时间】:2021-06-16 17:36:38
【问题描述】:

我有以下代码:

String url = // a valid url
String requestXml = File.ReadAllText(filePath);//opens file , reads all text and closes it
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(requestXml);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Credentials = new NetworkCredential("DEFAULT\\Admin", "Admin"); 
request.ContentType = "application/xml";
request.ContentLength = bytes.Length;
request.Method = "POST";
request.KeepAlive = false;
Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
return new StreamReader(responseStream).ReadToEnd();

在运行时,我在尝试读取 HTTPWebResponse 时遇到以下异常:

System.Net.WebException:底层连接已关闭:接收时发生意外错误。
---> System.IO.IOException:无法从传输连接中读取数据:已建立的连接被主机中的软件中止。
---> System.Net.Sockets.SocketException:已建立的连接被主机中的软件中止
在 System.Net.Sockets.Socket.Receive(Byte[] 缓冲区,Int32 偏移量,Int32 大小,SocketFlags socketFlags)
在 System.Net.Sockets.NetworkStream.Read(Byte[] 缓冲区,Int32 偏移量,Int32 大小)
在 System.Net.Sockets.NetworkStream.Read(Byte[] 缓冲区,Int32 偏移量,Int32 大小)
在 System.Net.PooledStream.Read(Byte[] 缓冲区,Int32 偏移量,Int32 大小)
在 System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead)

【问题讨论】:

  • @w0051977 我已经看过这个帖子了,我已经在设置 ​​request.keepAlive = false。那么是否需要重写GetWebRequest(Uri uri)方法呢?
  • 删除 Close() 方法并重试。您正在尝试从已关闭的连接中读取响应。

标签: c# .net rest http


【解决方案1】:

在读取之前不要关闭流。这应该有效:

String url = // a valid url
String requestXml = File.ReadAllText(filePath);//opens file , reads all text and closes it
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(requestXml);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Credentials = new NetworkCredential("DEFAULT\\Admin", "Admin"); 
request.ContentType = "application/xml";
request.ContentLength = bytes.Length;
request.Method = "POST";
request.KeepAlive = false;
using (Stream requestStream = request.GetRequestStream())
{
    requestStream.Write(bytes, 0, bytes.Length);
    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        using (Stream responseStream = response.GetResponseStream())
        {
            using (StreamReader streamReader = new StreamReader(responseStream))
            {
                return streamReader.ReadToEnd();
            }
        }
    }
}

【讨论】:

    【解决方案2】:

    添加这行代码解决了我的问题:

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
    

    来自:https://our.umbraco.com/forum/using-umbraco-and-getting-started/74628-the-underlying-connection-was-closed-an-unexpected-error-occurred-on-a-send

    【讨论】:

      【解决方案3】:
      using (var client = new HttpClient())
      {
          client.BaseAddress = new Uri(url);
          httpResponse = client.GetAsync(url).Result;
      }
      

      试试这个

      【讨论】:

        猜你喜欢
        • 2010-11-30
        • 1970-01-01
        • 1970-01-01
        • 2015-07-05
        • 2018-03-23
        • 2020-06-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多