none
Web request fails, but only for localhost and when using System.Net.HttpWebRequest RRS feed

  • Question

  • I've got a very weird problem with System.Net.HttpWebRequest, that's driving me nuts: When I run it with a localhost address, then it is either extremely slow (around 30 sec) or, in most cases, it times out entirely. This happens only with requests that target localhost, and only from C# code.

    Concretely, taking an 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();
    }

    ..it will most of the time fail with this exception:

    System.Net.WebException : Unable to connect to the remote server
      ----> System.Net.Sockets.SocketException : 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 127.0.0.1:64497

    By inspecting the .NET code, I tracked down the error to the WSAConnect API returning non-null value, but I can't see that this is of much help. (However, this is a 32bit API, whereas I'm running on a 64bit system. Could there be some strange 32/64bit problem?)

    When I run the above code with an address other than localhost (say http://www.google.com), then it behaves normally and I get response times around 1 sec. Also, when I submit the above request from a different client (I used Chrome's Advanced REST Client), then I get the normal behaviour: A delay time of about 5-8 sec for the first request due to initial compilation, and very short response times (20-40 msec) for all subsequent requests.

    Given these findings, it seems quite obvious to me that the problem must come from something that's specific to the implementation of the System.Net.HttpWebRequest class...

    The web application itself is hosted on IIS Express, but I'm quite sure that this is irrelevant, since the the above request never reaches the web server (I checked that both by running IIS Express from the command line and then inspecting the request coming through and also per ProcessMonitor).

    Further information: I'm running Win 8.1 64bit and VS 2013 SP4 pro. The system's HOST file is unmodified, i.e. it does not contain any entries.

    Things I've tried so far:

    • Running VS in admin mode
    • setting proxysettings explicitly to null, both per code and per configuration
    • inspecting the IIS Express process per ProcessMonitor - no result (as I said, I'm quite sure that the Web server is not directly involved in this problem)
    • changing localhost to 127.0.0.1

    I don't understand what the problem is, and I can't find any relevant info on the web. I don't even have any more ideas what I could try to track down the error. In other words: I'm totally stuck on this, and it effectively prevents me from writing any integration tests against locally hosted webs. Any help/hint/tip is highly appreciated.


    Wednesday, December 10, 2014 5:56 AM

All replies

  • Hello Weller,

    According to your description, I created a test with a Console application as client and a web api application. However, whether I host the webapi to iis express or the local iis, the response is returned successfully:

    The request code:

    static void Main(string[] args)
    
            {
    
                //WebRequest req = WebRequest.Create("http://localhost/WebApplication2/api/values");
    
                WebRequest req = WebRequest.Create("http://localhost:54543/api/values");
    
                req.Method = "GET";
    
                req.ContentType = "application/json; charset=utf-8";
    
                WebResponse resp = req.GetResponse();
    
                Stream stream = resp.GetResponseStream();
    
                StreamReader re = new StreamReader(stream);
    
                String json = re.ReadToEnd();
    
            }
    

    >>(However, this is a 32bit API, whereas I'm running on a 64bit system. Could there be some strange 32/64bit problem?)

    Also having a try to set webapi to 32bit and console application to 64 bit, and it still works fine.

    >>Given these findings, it seems quite obvious to me that the problem must come from something that's specific to the implementation of the System.Net.HttpWebRequest class...

    Not sure why it occurs the timeout exception in your side, my suggestion is you could debug it since now .net is open-sourced, you could follow this blog to debug into the GetResponse() method.

    >>Things I've tried so far:

    Do you have a try to host the web api application to the IIS rather than host it to the IIS express(Visual Studio)? If it works, then I am wondering when you host the webapi to the Visual Studio, it does not expose the service. Please also start up the two applications(run webapi firstly) at the same time to see if request works.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, December 10, 2014 8:41 AM
    Moderator
  • Hi Fred,

    thanks for your effort.

    Regarding your suggestions:

    • Hosting the site in IIS instead of IIS Express makes no difference.
    • I wasn't able to set up debugging for the .NET source code in this short period of time. But the exception together with code reading already contains all information: The problem occurs in 'System.Net.Sockets.Socket.DoConnect'  because the native API given above returns the error code 10060 (TimedOut).

    Oh, and just in case you are wondering: I also tried to shut down all firewall and antivirus software...

    Regards

    Thomas

    Wednesday, December 10, 2014 11:20 AM
  • Hello Thomas,

    I am wondering it might be caused by some features of the computer environment, if possible, try running your webapi server and client application on a different computer to see if this issue would disappear since in my side, my program environment should be similar with yours and it works.

    Regards.

    Fred.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, December 11, 2014 7:52 AM
    Moderator
  • Hi Fred,

    I was hoping I could avoid this big effort. But there seems no way around it :-(.

    I will setup a VM over the weekend and will come back to you next week.

    Until then, thanks again for your effort

    Thomas

    Friday, December 12, 2014 6:26 AM
  • Things I've tried so far:

    • setting proxysettings explicitly to null, both per code and per configuration



    Have you also tried ‘WebRequest.DefaultWebProxy = null’ before your code?


    Friday, December 12, 2014 7:50 AM
  • Yes, I also tried it that way.
    Friday, December 12, 2014 11:18 AM
  • Hello Thomas,

    Does the project work for you with running on the VM?

    Best Regards,

    Fred.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, December 17, 2014 5:16 AM
    Moderator
  • Hi again,

    things went a bit chaotic before Xmas/new year. So I didn't find the time to investigate the issue further until now. Sorry for the delay.

    Before setting up a VM, I thought I try running the above code in LinqPad, thus avoiding all the additional complexity that comes with Visual Studio and various related add-ins. 
    But that made no difference, I get the exact same timing behavior on LinqPad: Either response times around 40 sec or exceptions as described above. So Visual Studio does not seem to be the problem - at least not directly.

    However, the problem did not exist on the VM. The response from IIS Express was almost immediate (using the same LinqPad approach than described above).

    So there must be something on my machine. I suspect it indirectly has something to do with the OS' .NET framework in combination with the VS2013 environment. But there are thousands of possibilities, and I don't even have an idea where I should begin searching...
    Sunday, January 4, 2015 6:32 PM
  • Hello,

    Not sure if it is that the.NET Framework is not be installed correctly, you could download this tool try to repair the .NET Framework to see if it works:

    http://www.microsoft.com/en-hk/download/details.aspx?id=30135

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, January 5, 2015 5:31 AM
    Moderator
  • Hi again,

    after trying around for many frustrating hours, I finally found the problem.

    First I tried the .NET framework repair tool that you recommended. No result.

    Turned out that it was my AntiVirus scanner (ZoneAlarm). But not in an obvious way. There's a hidden process that continues to run even when you suspend the antivirus scanner and firewall (and you also cannot end the task from within Windows Task Manager...). The only thing that works so far is to manually shut down ZoneAlarm before running the code - which is of course not an option. But anyway, I will contact ZoneAlarm support to see how to best handle this...

    Do you know what process is actually executing the request? Knowing that will surely help me to configure the service (if that should be possible).

    Regards
    Thomas
    Monday, January 5, 2015 9:29 AM
  • Hello,

    >>Do you know what process is actually executing the request?

    For this, i think this blog could be helpful, it describes how to find the suspend thread by using Windbg. In your case, you could provide a long enough timeout value and follow that blog to find which thread is hang so that it occurs the timeout exception.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, January 6, 2015 8:51 AM
    Moderator