shasi
shasi

Reputation: 71

System.Net.Sockets.SocketException (10060): A connection attempt failed because the connected party did not properly respond after a period of time

I have a functionality to call rest services from my web application. I am doing this in c#.net code

My Code

using (var client = new HttpClient())
 {
  client.DefaultRequestHeaders.Accept.Add(new 
 MediaTypeWithQualityHeaderValue("application/json"));

  var byteArray = Encoding.ASCII.GetBytes("abc:defg!");

  client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));

  client.DefaultRequestHeaders.Add("CSRF_NONCE", a.NonceValue);

   var message = await client.PostAsync("hostname/Windchill/servlet/odata/v3/ProdMgmt/Parts('OR:wt.part.WTPart:123456')/PTC.ProdMgmt.GetPartStructure?$expand=Components($select=PartName,PartNumber;$expand=PartUse($select=FindNumber,LineNumber,Quantity,Unit);$levels=1)", null);
  }
  1. I can call it successfully from my local machine but when I try to call it after deploying to the Test server (Windows Server), I get the below error.

    System.Net.Http.HttpRequestException: 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. ---> 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|283_0(AwaitableSocketAsyncEventArgs saea, ValueTask connectTask, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.DefaultConnectAsync(SocketsHttpConnectionContext context, CancellationToken cancellationToken) at System.Net.Http.ConnectHelper.ConnectAsync(Func3 callback, DnsEndPoint endPoint, HttpRequestMessage requestMessage, CancellationToken cancellationToken) --- End of inner exception stack trace --- at System.Net.Http.ConnectHelper.ConnectAsync(Func3 callback, DnsEndPoint endPoint, HttpRequestMessage requestMessage, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.GetHttpConnectionAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken) at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken) at System.Net.Http.DiagnosticsHandler.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken) at System.Net.Http.HttpClient.SendAsyncCore(HttpRequestMessage request, HttpCompletionOption completionOption, Boolean async, Boolean emitTelemetryStartStop, CancellationToken cancellationToken)

I tried doing the below to make it functional but nothing has worked.

  1. "A connection attempt failed because the connected party did not properly respond after a period of time" using WebClient

  2. Added the below code in the constructor method

ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;

  1. My Test server uses proxy settings in the browser to access the rest services url. So I tried adding Proxy details in my code.

WebProxy proxy = new WebProxy { Address = new Uri($"http://x.x.x.x:xxxx"), }; ServicePointManager.Expect100Continue = false; ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; HttpClientHandler clientHandler = new HttpClientHandler() { AllowAutoRedirect = true, AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip, Proxy = proxy, }; using (var client = new HttpClient(clientHandler))

Any help is much appreciated.

Upvotes: 5

Views: 46365

Answers (1)

Cory Nelson
Cory Nelson

Reputation: 30031

The key is to look at the "10060" part, which is socket error code WSAETIMEDOUT.

This indicates a TCP socket is unable to connect because the remote party didn't respond.

Check that the hostname/port is correct, server is online, firewall isn't dropping packets etc.

Upvotes: 4

Related Questions