HttpRequestException -- Is this a client or server issue?
<p>Awhile ago I implemented some code to consume a REST Api using the <code>HttpClient</code> class.</p>

<pre><code>using (var client = new HttpClient() { BaseAddress = new Uri(@"https://thirdparty.com") })
{
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(...);

    var uri = new Uri(@"rest/api/foo", UriKind.Relative);
    var content = new StringContent(json.ToString());

    using (var response = await client.PostAsync(uri, content))
    {
        // etc ...
    }
}
</code></pre>

<hr>

<p>This code seemed to work perfectly fine against both the test and production environments (each of which access a test/production uri). Recently, we started to get an HttpRequestException in the <strong>production environment only</strong>: <code>System.Net.Http.HttpRequestException: Error while copying content to a stream.</code></p>

<p>This seemed a bit strange, so I used Postman to send the same message and it worked just fine. I wasn't sure why our code was failing and Postman was working. I changed a parameter in the json data (the state from "NY" to "NV") and our .NET code worked fine -- of course we can't just send the wrong json data, so this isn't a solution; this was more of an observation that the exact same code worked fine with different content.</p>

<hr>

<p>What's interesting is that there are two code changes we could make that will resolve this. Firstly, Postman is able to generate working C# code using the <a href="https://www.nuget.org/packages/RestSharp" rel="nofollow noreferrer"><code>RestSharp</code></a> package. Alternatively, I found an <a href="https://stackoverflow.com/a/33501680">answer</a> from another question pointing to using HttpVersion 1.0:</p>

<pre><code>using (var request = new HttpRequestMessage(HttpMethod.Post, uri))
{
    request.Version = HttpVersion.Version10;
    request.Content = new StringContent(json.ToString());

    using (var response = await client.SendAsync(request))
    {
        // etc ...
    }
}
</code></pre>

<p>The confusing part is that Postman uses the HTTP/1.1 version. So, in summary:</p>

<ul>
<li>If we change the json data (US State from "NY" to "NV"), the code works.</li>
<li>The same exact json and code works against the test uri.</li>
<li>Changing the code to use the RestSharp package works.</li>
<li>Changing the code to use HTTP/1.0 instead of HTTP/1.1 works.</li>
</ul>

<p>Why on earth is Postman able to work using HTTP/1.1 but HttpClient fails? Is this a problem with our client (the code works for other US States)? Is this a bug in the .NET Framework? Is there something wrong with the implementation/hosting of the REST Api from the third party?</p>

<hr>

<p>Postman headers:</p>

<pre><code>POST rest/api/foo HTTP/1.1
Host: thirdparty.com
Content-Type: application/json
Authorization: Basic SOME_ENCRYPTED_USER_PASS
Cache-Control: no-cache
Postman-Token: 2fa5b5a0-b5d3-bd4c-40f0-d2b55b60316b
</code></pre>

<p>Sample Json:</p>

<pre><code>{
    "stateCode": "NY",
    "packageID": "58330",
    "name": "58330-PRI-1",
    "documents": [
        {
            "type": "SPECIAL",
            "name": "Sample Document",
            "documentID": "3569976"
        }
    ],
    "descriptions": [
        {
            "city": "New York",
            "state": "NY"
        }
    ]
}
</code></pre>

<p>Stacktrace:</p>

<pre><code>AggregateException: One or more errors occured.
HttpRequestException: Error while copying content to a stream.
IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
SocketException: An existing connection was forcibly closed by the remote host.
</code></pre>