C#:System.Net.WebException:底层连接已关闭 [英] C# : System.Net.WebException: The underlying connection was closed

查看:522
本文介绍了C#:System.Net.WebException:底层连接已关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

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();

在运行期间,我在尝试读取<$ c $时遇到以下异常c> HTTPWebResponse :

During run-time, I'm getting the following exception while trying to read the HTTPWebResponse :


System.Net.WebException:基础连接已关闭:发生意外错误接收。

---> System.IO.IOException:无法从传输连接读取数据:已建立的连接被主机中的软件中止。

- - > System.Net.Sockets.SocketException:已建立的连接被主机中的软件中止了

在System.Net.Sockets.Socket.Receive(Byte []缓冲区,Int32偏移量,Int32 size,SocketFlags socketFlags)

在System.Net.Sockets.NetworkStream.Read(Byte []缓冲区,Int32偏移量,Int32大小)

在System.Net.Sockets.NetworkStream。读取(Byte []缓冲区,Int32偏移量,Int32大小)

在System.Net.PooledStream.Read(Byte []缓冲区,Int32偏移量,Int32大小)

在System.Net.Connection.SyncRead(HttpWebRequest请求,布尔userRetrievedStream,布尔probeRead)

System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
---> System.IO.IOException: Unable to read data from the transport connection: An established connection was aborted by the software in your host machine.
---> System.Net.Sockets.SocketException: An established connection was aborted by the software in your host machine
at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead)


推荐答案

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

Don't close the stream before reading from it. This should work:

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();
            }
        }
    }
}

这篇关于C#:System.Net.WebException:底层连接已关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆