We have a .NET 6 application which makes requests to a third party API.
About a month ago, requests from most of our computers started failing. The error we get is:
System.Net.Http.HttpRequestException: An error occurred while sending
the request. —> System.IO.IOException: The response ended
prematurely.
That’s it, that’s the whole error. No inner exception, unless I don’t know how to log it properly.
We used Wireshark to check packet flow and it seems that the request does not leave the computer.
Only thing that has changed is the third party API started using Cloudflare.
Things that have been tested and didn’t help:
- Update Windows version to 22H2
- Install all Windows updates
- Check firewall
- Compare registry with IIS Crypto
- Reinstall our application
- Compare .NET runtimes and make them equal
- Try to launch our app as self-contained and see if that changes anything
- Check if our external IP is blocked by the third party
- Check TLS (requests failed with 1.1, 1.2, 1.3)
- Check computer firewall
- Windows proxy – it’s off
- Network adapter settings
- ipconfig /flushdns
- Reset windows proxy
What I also tried is to make a .NET console application and just send a request straight to the third party.
Code:
try
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
ServicePointManager.Expect100Continue = true;
ServicePointManager.DefaultConnectionLimit = 9999;
var apiUrl = "{apiUrl}";
var httpClient = new ServiceCollection()
.AddHttpClient()
.BuildServiceProvider()
.GetService<IHttpClientFactory>()
.CreateClient();
httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("text/xml"));
httpClient.Timeout = new TimeSpan(0, 0, 30);
var httpContent = new StringContent(body, Encoding.UTF8, "text/xml");
var httpRequest = new HttpRequestMessage
{
//Version = HttpVersion.Version20,
RequestUri = new Uri($"{apiUrl}PharmacyClaim/RegisterClaim"),
Content = httpContent,
Method = HttpMethod.Post
};
Log.Logger.Information("Making request to {0}, content:{1}{2}", httpClient.BaseAddress + "PharmacyClaim/RegisterClaim", Environment.NewLine, body);
var response = await httpClient.SendAsync(httpRequest);
var content = await response.Content.ReadAsStringAsync();
Log.Logger.Information("Response content: {0}{1}", Environment.NewLine, content);
Log.Logger.Information("Response info: {0} HTTP version: {1}", Environment.NewLine, response.Version);
}
catch (HttpRequestException e)
{
Log.Logger.Error(e.InnerException, "Caught inner exception: {0}", e.InnerException?.Message);
throw;
}
catch (Exception ex)
{
Log.Logger.Error(ex, "Caught exception: {0}", ex.Message);
throw;
}
Body is just an XML string.
I’m getting the same error, but what I could figure out is:
-
setting HTTP protocol version to 2.0 fixes the problem, but in some computers the request works with HTTP 1.1, so this really is just fixing the cause, not the reason
-
launching fiddler in PC makes the request work – I’ve read a bit about this and it seems like fiddler makes some kind of proxy for requests? No idea why using it makes the request work, but it seems like Fiddler alleviates a lot of .NET HTTP problems
-
using Postman to make requests works
-
using Curl to make requests works
The strangest thing is that in some computers the requests work, but in most of them they started failing.
Is this a Cloudflare issue, a .NET one, network issue? Can I get a more precise exception?
Any and all help would be appreciated.
1