C# 请求被中止:连接意外关闭

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11161516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 16:49:16  来源:igfitidea点击:

The request was aborted: The connection was closed unexpectedly

c#asp.netweb-serviceshttpwebrequest

提问by Xaisoft

I am using Jon Skeet's ReadFully method implemented here:

我正在使用在此处实现的 Jon Skeet 的 ReadFully 方法:

public static byte[] ReadFully(Stream stream)
{
    var buffer = new byte[32768];
    using (var ms = new MemoryStream())
    {
        while (true)
        {
            int read = stream.Read(buffer, 0, buffer.Length);
            if (read <= 0)
                return ms.ToArray();
            ms.Write(buffer, 0, read);
        }
    }
}

It throws an exception at the line:

它在该行引发异常:

int read = stream.Read(buffer, 0, buffer.Length);

The error message is The request was aborted: The connection was closed unexpectedly.

错误信息是The request was aborted: The connection was closed unexpectedly

I am sending an xml request to a webservice. My send method looks like this:

我正在向 Web 服务发送 xml 请求。我的发送方法如下所示:

private static string SendRequest(XElement request, string url)
{
    var req = (HttpWebRequest)WebRequest.Create(url);
    req.ContentType = "application/soap+xml;";
    req.Method = "POST";
    req.KeepAlive = false;
    req.Timeout = System.Threading.Timeout.Infinite;
    req.ReadWriteTimeout = System.Threading.Timeout.Infinite;
    req.ProtocolVersion = HttpVersion.Version10;
    req.AllowWriteStreamBuffering = false;

    using (var stm = req.GetRequestStream())
    {
        using (var stmw = new StreamWriter(stm))
        {
            stmw.Write(request.ToString());
        }
    }

    Stream responseStream;
    using (var webResponse = req.GetResponse())
    {
        responseStream = webResponse.GetResponseStream();
    }

    // Do whatever you need with the response
    var myData = ReadFully(responseStream);
    string responseString = Encoding.ASCII.GetString(myData);

    return responseString;
}

I tried without and without the following variables set and it gives me the same message:

我在没有和没有设置以下变量的情况下尝试过,它给了我相同的消息:

req.KeepAlive = false;
req.Timeout = System.Threading.Timeout.Infinite;
req.ReadWriteTimeout = System.Threading.Timeout.Infinite;
req.ProtocolVersion = HttpVersion.Version10;
req.AllowWriteStreamBuffering = false;

采纳答案by Blindy

The problem is in this part of your code:

问题出在您代码的这一部分:

// wrong way to do it!
Stream responseStream;
using (var webResponse = req.GetResponse())
{
    responseStream = webResponse.GetResponseStream();
}

// Do whatever you need with the response
var myData = ReadFully(responseStream);

You're disposing your response object before reading from its stream. Try something like this instead:

在从其流中读取之前,您正在处理您的响应对象。试试这样的:

byte[] myData;
using (var webResponse = req.GetResponse())
{
    var responseStream = webResponse.GetResponseStream();
    myData = ReadFully(responseStream);    // done with the stream now, dispose of it
}

// Do whatever you need with the response
string responseString = Encoding.ASCII.GetString(myData);