System.Net.Http.HttpRequestException Error while copying content to a stream

后端 未结 3 2145
广开言路
广开言路 2020-12-14 17:10

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%

相关标签:
3条回答
  • 2020-12-14 17:49

    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
    
    0 讨论(0)
  • 2020-12-14 18:02

    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);
    }
    
    0 讨论(0)
  • 2020-12-14 18:06

    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.
    
    0 讨论(0)
提交回复
热议问题