服务器在写入响应标头后关闭连接时的HttpClient异常

时间:2014-01-22 14:14:24

标签: c# http dotnet-httpclient

我使用HttpClient使用PutAsync将数据PUT到HTTP服务器。

当服务器发回错误代码(例如401 Unauthorized)时,它会关闭连接(在其响应中提供了Connection:close标头)。当发生这种情况时,HttpClient会抛出System.Net.Http.HttpRequestException(将内容复制到流时出错),并列出下面列出的内部异常。这似乎与写入数据有关(尽管事实上它在BeginRead中),因为如果我说服服务器读取所有正在发送的数据,那么就不会抛出异常。

我可以使用HttpClient.SendAsync使用HttpCompletionOption.ResponseHeadersRead参数执行相同的任务来解决此问题。在这种情况下,不会抛出异常,我可以阅读HttpResponseMessage.StatusCode

我的问题是,异常是正确的吗?即,服务器没有遵循标准,关闭连接而不读取它不需要的所有数据?如果没有,那么PutAsync是否存在问题,因为在这种情况下它不允许访问返回StatusCode

示例代码:

using (FileStream stream = File.OpenRead(FilePath))
using (StreamContent content = new StreamContent(stream))
using (HttpRequestMessage req = new HttpRequestMessage
    {
        Method = HttpMethod.Put,
        RequestUri = new Uri(baseUri, Path),
        Content = content,
    })
using (HttpClient client = new HttpClient())
{
    req.Content.Headers.ContentType = new MediaTypeHeaderValue(FileContentType);
    // This version works:
    //using (HttpResponseMessage resp = await client.SendAsync(req,
    //    HttpCompletionOption.ResponseHeadersRead))
    // This version throws an exception:
    using (HttpResponseMessage resp = await client.PutAsync(
        new Uri(baseUri, Path), content))
    {
        if (resp.StatusCode == HttpStatusCode.Unauthorized)
        {
            // Try again with authorisation credentials based on resp.Headers.WwwAuthenticate
        }
    }
}

内部异常:System.IO.IOException(无法从传输连接读取数据:远程主机强行关闭现有连接。)

at System.Net.Sockets.NetworkStream.BeginRead(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state)
at System.Net.PooledStream.BeginRead(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state)
at System.Net.ConnectStream.BeginReadWithoutValidation(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state)
at System.Net.ConnectStream.BeginRead(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state)
at System.Net.Http.HttpClientHandler.WebExceptionWrapperStream.BeginRead(Byte[] buffer, Int32 offset, Int32 count, AsyncCallback callback, Object state)
at System.Net.Http.StreamToStreamCopy.StartRead()

内部异常:System.Net.Sockets.SocketException(远程主机强行关闭现有连接)

at System.Net.Sockets.Socket.BeginReceive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, AsyncCallback callback, Object state)
at System.Net.Sockets.NetworkStream.BeginRead(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state)

1 个答案:

答案 0 :(得分:0)

如果符合您的需要,请尝试使用HTTP协议版本1.0:

using (HttpRequestMessage req = new HttpRequestMessage
    {
        Method = HttpMethod.Put,
        RequestUri = new Uri(baseUri, Path),
        Content = content,
        Version = HttpVersion.Version10
    })