Home > Net >  HttpClient socket exception
HttpClient socket exception

Time:06-16

I am trying to call some external API hosted on azure from my web Api. The API is working fine on my local machine but when I deploy it IIS on server it starts throwing System.Net.Sockets.SocketException (10060).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. Although I have increased the request timeout to 5 minutes but connection is silently stopping after 21 seconds and throwing aforementioned exception.

Here is my code:

var telemetries = new TelemetryResponse();
        var client = httpClientFactory.CreateClient("Lynx");
        client.Timeout = TimeSpan.FromMinutes(5);
        var httpResponseMessage = await client.GetAsync("vehicletelemetries/All?key=iLJIbAVXOnpKz5xyF0zV44yepu5OVfmZFhkHM7x");

        if (httpResponseMessage.IsSuccessStatusCode)
        {
            string content = await httpResponseMessage.Content.ReadAsStringAsync();
            telemetries = JsonConvert.DeserializeObject<TelemetryResponse>(content);
        }

Exception I am getting is:

System.Net.Sockets.SocketException (10060): 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. at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken) at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token) at System.Net.Sockets.Socket.g__WaitForConnectWithCancellation|277_0(AwaitableSocketAsyncEventArgs saea, ValueTask connectTask, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.ConnectToTcpHostAsync(String host, Int32 port, HttpRequestMessage initialRequest, Boolean async, CancellationToken cancellationToken)

CodePudding user response:

You got an error code:

WSAETIMEDOUT 10060

Connection timed out. A connection attempt failed because the connected party did not properly respond after a period of time, or the established connection failed because the connected host has failed to respond.

https://docs.microsoft.com/en-us/windows/win32/winsock/windows-sockets-error-codes-2#WSAETIMEDOUT

Check your network connection, ip adress, port and maybe the connection got blocked from a firewall.

CodePudding user response:

Hi guys the culprit was the proxy and we have to configure our HttpClient with proxy while creating it/registering in the DI container. I have registered the HttpClient in DI like this.

var proxySettings = new ProxySetting();
    Configuration.Bind(nameof(ProxySetting), proxySettings);

    services.AddHttpClient("Lynx", client =>
    {
        client.BaseAddress = new Uri(Configuration.GetSection("LynxUrl").Value);
    }).ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler { Proxy = new MyProxy(proxySettings)});

And my proxy code is

public class MyProxy : IWebProxy
{
private readonly ProxySetting _proxySetting;

public MyProxy(ProxySetting proxySetting)
{
    _proxySetting = proxySetting;
}
public ICredentials Credentials
{
    //get { return new NetworkCredential("username", "password"); }
      get { return new NetworkCredential(_proxySetting.UserName, _proxySetting.Password,_proxySetting.Domain); }
      set { }
}

public Uri GetProxy(Uri destination)
{
    return new Uri(_proxySetting.ProxyUrl);
}

public bool IsBypassed(Uri host)
{
    return false;
}

}

It has resolved my problem completely.

  • Related