By jonho


2015-10-20 10:07:57 8 Comments

I am using the HttpClient class in .NET Framework 4.5.2.

I calling PostAsync against a third party web service. 80% of the time this post works, 20% of the time our response is cut short. In this situation we get the following exception:

System.Net.Http.HttpRequestException: Error while copying content to a stream. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host at System.Net.Sockets.NetworkStream.BeginRead(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state) --- End of inner exception stack trace --- at System.Net.Sockets.NetworkStream.BeginRead(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state) at System.Net.FixedSizeReader.StartReading() at System.Net.Security._SslStream.StartFrameHeader(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest) at System.Net.Security._SslStream.StartReading(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest) at System.Net.Security._SslStream.ProcessRead(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest) at System.Net.TlsStream.BeginRead(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback asyncCallback, Object asyncState) 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()

A subsequent identical request succeeds.

We cannot retry this request as the business action has already been taken. So it leaves us in an awkward situation.

This is my code:

using (var httpClient = new HttpClient())
{
    httpClient.DefaultRequestHeaders.Authorization = authorizationHeader;
    HttpContent httpContent = new StringContent(someXml);

    //Exception occurs on next line...
    var response = await httpClient.PostAsync("https://thirdpartyendpoint", httpContent);
    var responseXml = await response.Content.ReadAsStringAsync();  
    //convert to Dto              
}

The third-party service are successfully saving the record to their database and do not see any obvious exceptions at their end. They did note that the failing requests generally took longer (around 18-30 seconds) to write to the database than the successful requests.

What can I do to handle this better?

3 comments

@kimphamg 2019-04-20 15:33:15

I had the same error (Error while copying content to a stream) with HTTPClient PutAsync() method:

using (StreamContent content = new StreamContent(stream))
{
    HttpResponseMessage response = await client.PutAsync(url, content))
}

You need to specify the HttpCompletionOption.ResponseHeadersRead flag which is not available in PutAsync so I switched to SendAsync:

using (StreamContent content = new StreamContent(stream))
{
    var httpRequest = new HttpRequestMessage(HttpMethod.Put, url);
    httpRequest.Content = content;

    HttpResponseMessage response = await client.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead);
}

@user2173353 2019-06-27 14:47:04

Any idea why this is needed? Based on the description in the docs, I don't understand why this would help: docs.microsoft.com/en-us/uwp/api/….

@Wietze 2019-07-02 12:08:49

@Steve Wranovsky 2019-02-14 16:53:21

I had a similar problem with the use of a shared HttpClient connecting to a server for REST calls. The problem ended up being a mismatch between the KeepAlive timeout on the client and server. The client side timeout is set by the MaxServicePointIdleTime setting on the ServicePointManager and defaults to 100s. The server side idle timeout was set to a shorter value in our server.

Having a shorter timeout on the server as compared to the client resulted in the server sporadically closing a connection just when the client was attempting to connect. This resulted in the reported exception.

Note that I ultimately found the problem because I also received this exception under the same conditions:

System.Net.WebException: The underlying connection was closed: A connection that was expected to be kept alive was closed by the server.

@user2173353 2019-06-28 14:38:16

This sounds interesting. Can you say what you did to set the server idle timeout? For the client I guess you used ServicePointManager.FindServicePoint(...).

@Steve Wranovsky 2019-07-18 18:32:05

@user2173353, we had a customer HttpServer that we could set the KeepAlive directly on a socket. I'm not sure how to set it in other cases.

@jonho 2015-11-03 14:32:57

we resolved this problem with 2 code changes:

  1. Dispose of the httpResponseMessage and just work with a simple DTO

     using (var httpResponseMessage = await httpClient.SendAsync(httpRequestMessage))
        {
            return await CreateDto(httpResponseMessage);
        }
    
  2. Downgrade the version of HTTP to v1.0

    var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, new Uri(url))
    {
        Version = HttpVersion.Version10,
        Content = httpContent
    };
    
    await client.SendAsync(httpRequestMessage);
    

which has the effect of adding this Http header

    Connection: close 

rather than this

    Connection: keep-alive

@garenyondem 2016-03-07 13:38:55

Any ideas about how to apply HttpVersion to httpClient get requests?

@jonho 2016-03-14 22:18:30

var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, new Uri(url)) { Version = HttpVersion.Version10, Content = httpContent }; await client.SendAsync(httpRequestMessage); should do it

@zinczinc 2018-05-20 23:06:26

Using DTOs instead solved my problem. I suspected it would help. Thank you!

@juanora 2018-05-29 13:30:41

just by changing DefaultRequestHeaders.ConnectionClose to true worked for me :)

@Robert 2018-11-29 03:09:54

Downgrade to HTTP v1.0 worked for me. I found this is needed when post to Windows Server 2008 R2 but not Windows Server 2012, Any idea why?

@Mark Lauter 2019-03-16 01:30:30

I was using a long lived static HttpClient. You're comment about Connection: close and keep-alive was what I needed to see what was going wrong.

@user2173353 2019-06-27 14:43:49

But this might create performance side-effects, since you cannot use persistent connections. See here: stackoverflow.com/a/247026/2173353.

Related Questions

Sponsored Content

1 Answered Questions

5 Answered Questions

[SOLVED] Getting EOF exception over https call

  • 2011-03-21 19:26:15
  • Amzath
  • 38716 View
  • 17 Score
  • 5 Answer
  • Tags:   .net http

2 Answered Questions

[SOLVED] Accessing Webservice which is hosted on https server

Sponsored Content