Search code examples
c#asp.netdotnet-httpclient

.NET HttpClient does not timed out for in non console app


I need to run an ASP.NET WebAPI (.NET Framework 4.6.1) application in a network which is behind a firewall and I need to call some APIs which are hosted out of that network. I can hit those API with Postman from the same machine without any issue, but the HTTP client GetAsync method times out and fails.

I have created multiple applications using Visual Studio templates and put the following code in them (basically nothing expect the template code and following lines):

 var c = new HttpClient();
 var r = await c.GetAsync("https://google.com");

Above code works fine with .NET Framework 4.6.1 Console Application but fails on all other application types including:

  • ASP.NET Web Application .NET Framework 4.6.1
  • ASP.NET Core Web Application 2.2
  • .NET Core Console Application 2.2

I've also tried to bypass SSL cert using below code without any luck.

httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; };

For sure something on the network causing this, but what, hand how does this work in a .NET Framework and not any other one? I've tired public sites like google.com and I got the same result. Has anyone run to the same issue? any suggestion on how to debug it?


Solution

  • I could find the solution for .NET Framework Web Application. By adding below to the web.config, the default proxy would be set.

    <system.net>
       <defaultproxy/>
    <system.net>
    

    Reference: Configure the Hypertext Transfer Protocol (HTTP) proxy server

    But I do not have a working solution for .NET Core application yet.

    Update 4/9/2020
    I could fix the issue in .NET Core 2.x but setting config but this issue is fixed in .NET Core 3.x. Now HttpClient uses the default proxy by default.