Using .NET Core 3.0 I am trying to get the httpResponseMessage.StatusCode of:
var sw = Stopwatch.StartNew();
var httpClient = new HttpClient();
// latest Chrome user-agent doesn't work
const string userAgent = @"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36";
// Curl User-Agent works after 10s
//const string userAgent = "curl/7.55.1";
httpClient.DefaultRequestHeaders.Add("User-Agent", userAgent);
// Trying to get keep-alive header working
//httpClient.DefaultRequestHeaders.Add("Connection", "close");
//httpClient.DefaultRequestHeaders.Add("Connection", "keep-alive");
httpClient.DefaultRequestHeaders.Add("Connection", "Keep-Alive");
var url = "https://www.element14.com/community/community/designcenter/azure-sphere-starter-kits?ICID=azure-sphereCH-topbanner";
try
{
var httpResponseMessage = await httpClient.GetAsync(url);
var sc = (int)httpResponseMessage.StatusCode;
}
catch (HttpRequestException ex)
{
// always fails with Unable to read data from the transport connection:
// A connection attempt failed because the connected party did not properly respond after a period of time,
// or established connection failed because connected host has failed to respond..
sw.Stop();
// always failing around 19.3 or 19.5ms
}
Using
curl -I http://www.element14.com/community/community/designcenter/azure-sphere-starter-kits?ICID=azure-sphereCH-topbanner
We can see it is behind an F5 Big-IP load balancer which has been an issue with HttpClient:
https://github.com/dotnet/corefx/issues/7812#issuecomment-305848835
and from the F5 side:
Error Update
Full error text is: "A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond."
Error screenshot in VS2019 is:
Fiddler Update
The request working from Chrome.
The request failing from HttpClient. Notice the Connection: keep-alive difference
https://stackoverflow.com/a/38648237/26086 - setting the headers I couldn't see any difference from Fiddler.
Curl Update
Passing the curl User-Agent works albeit slowly (10s).