-
Notifications
You must be signed in to change notification settings - Fork 5.4k
HttpWebRequest hits 1s timeout when resolving http://localhost on Win10 (.NET Framework is fast) #23581
Description
Consider the following method (error/exception handling omitted for brevity:
public virtual string MakeHttpRequest(Uri fullUri, string httpMethod, string requestBody)
{
string responseString = string.Empty;
HttpWebRequest request = HttpWebRequest.Create(fullUri) as HttpWebRequest;
request.Method = httpMethod;
request.Timeout = 30000;
request.Accept = "application/json,image/png";
if (request.Method == "POST")
{
string payload = requestBody;
byte[] data = Encoding.UTF8.GetBytes(payload);
request.ContentType = "application/json;charset=utf-8";
Stream requestStream = request.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();
}
HttpWebResponse webResponse = request.GetResponse() as HttpWebResponse;
using (Stream responseStream = webResponse.GetResponseStream())
using (StreamReader responseStreamReader = new StreamReader(responseStream, Encoding.UTF8))
{
responseString = responseStreamReader.ReadToEnd();
}
return responseString;
}Executing this method against a server running on localhost yields wildly different execution times when run via the full .NET Framework vs. .NET Core. I have a demo project that shows the discrepancy, making calls to a locally running HTTP server, that server being the Chromium project's chromedriver.exe, used to automate the Chrome browser. When running the executables created by that project, you can see the following typical outputs (as run on Windows 10, build 16288):
Run against .NET Core 2.0:
Starting test...
Starting ChromeDriver 2.32.498550 (9dec58e66c31bcc53a9ce3c7226f0c1c5810906a) on port 9515
Only local connections are allowed.
Started session 946394531499259b7442073c0e26060d
Navigating to http://www.google.com
Navigation complete
Making 10 HTTP calls to localhost, logging the elapsed time...
Elapsed time for HTTP call: 1038 milliseconds
Elapsed time for HTTP call: 1016 milliseconds
Elapsed time for HTTP call: 1015 milliseconds
Elapsed time for HTTP call: 1011 milliseconds
Elapsed time for HTTP call: 1012 milliseconds
Elapsed time for HTTP call: 1024 milliseconds
Elapsed time for HTTP call: 1013 milliseconds
Elapsed time for HTTP call: 1027 milliseconds
Elapsed time for HTTP call: 1013 milliseconds
Elapsed time for HTTP call: 1014 milliseconds
Test finished. Press to exit.
Running against the full .NET Framework 4.5:
Starting test...
Starting ChromeDriver 2.32.498550 (9dec58e66c31bcc53a9ce3c7226f0c1c5810906a) on port 9515
Only local connections are allowed.
Started session 81c2bbd21a0d89354b2dead8d52ee982
Navigating to http://www.google.com
Navigation complete
Making 10 HTTP calls to localhost, logging the elapsed time...
Elapsed time for HTTP call: 35 milliseconds
Elapsed time for HTTP call: 7 milliseconds
Elapsed time for HTTP call: 12 milliseconds
Elapsed time for HTTP call: 5 milliseconds
Elapsed time for HTTP call: 4 milliseconds
Elapsed time for HTTP call: 7 milliseconds
Elapsed time for HTTP call: 4 milliseconds
Elapsed time for HTTP call: 6 milliseconds
Elapsed time for HTTP call: 5 milliseconds
Elapsed time for HTTP call: 5 milliseconds
Test finished. Press to exit.
I would expect similar performance between framework versions. Absent that, I would expect there to be settings to modify to the HttpWebRequest object that would yield similar performance as a workaround.
[EDIT] C# syntax highlighing by @karelz