I have a very strange problem with System.Net.HttpWebRequest that turns me on: When I run it with the localhost address, it is either very slow (about 30 seconds), or, in most cases, it takes time completely. This only happens with requests targeting localhost , and only with C # code.
Specifically, taking the ASP.NET Web API project as an example, when I have this code:
[Test] public void TestRequest() { var request = WebRequest.Create("http://localhost:64497/api/values"); WebResponse response = request.GetResponse(); }
.. in most cases this failure is excluded:
System.Net.WebException: Unable to connect to the remote server
----> System.Net.Sockets.SocketException: the connection attempt failed because the connected party did not respond properly after a period of time or the connection was established because the connected host could not respond 127.0.0.1:64497
Checking the .NET code, I found an error in the WSAConnect API , returning a non-zero value, but I can’t help much. (However, this is a 32-bit API, while I am working on a 64-bit system. Could there be some strange problem with 32/64-bit?)
When I run the above code with an address other than localhost (say http://www.google.com ), then it behaves normally, and I get a response time of about 1 second. In addition, when I send the above request from another client (I used the Chrome Advanced REST Client ), then I get the normal behavior: the delay time is about 5-8 seconds for the first request due to the initial compilation and very short response time (20-40 ms) for all subsequent requests.
Given these conclusions, it seems to me completely obvious that the problem should come from something specific for the implementation of the System.Net.HttpWebRequest class ...
The web application itself is hosted in IIS Express, but I’m absolutely sure that it doesn’t matter, since the above request never reaches the web server (I checked it both using IIS Express from the command line and after checking the requests, passing through ProcessMonitor).
Additional information: I am running Win 8.1 64bit and VS 2013 SP4 pro. The system HOST file is not changed, that is, it does not contain any entries.
Things I've tried so far:
- Running VS in Admin Mode
- Disabling all firewalls and antivirus programs
- using IIS instead of IIS Express
- explicitly point proxies to zero, both for each code and for each configuration
- checking the IIS Express process for ProcessMonitor - there is no result (as I said, I'm quite sure that the web server is not directly involved in this problem)
- change
localhost to 127.0.0.1
I don’t understand what the problem is, and I can’t find the relevant information on the Internet. I don’t even have any ideas that I could try to track down the error. In other words: I am completely stuck with this, and it actually prevents me from writing any integration tests against locally hosted websites. Any help / hint / hint is greatly appreciated.