C# – WebException: Unable to connect to the remote server

cexceptionsocketsweb services

I have a client application that uses WebHttpRequest to get response from external server, it works fine but some times I'm getting the following 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
xx.xx.xxx.xxx:xxx at
System.Net.Sockets.Socket.DoConnect(EndPoint
endPointSnapshot, SocketAddress
socketAddress) at
System.Net.Sockets.Socket.InternalConnect(EndPoint
remoteEP) at
System.Net.ServicePoint.ConnectSocketInternal(Boolean
connectFailure, Socket s4, Socket s6,
Socket& socket, IPAddress& address,
ConnectSocketState state, IAsyncResult
asyncResult, Int32 timeout, Exception&
exception) — End of inner
exception stack trace —

at
System.Net.HttpWebRequest.GetRequestStream(TransportContext&
context)

at
System.Net.HttpWebRequest.GetRequestStream()

…. Any help?

Best Answer

This is more of a "Request Timed Out" error. The server you are trying to connect to did not respond to your request. You should handle this exception using try-catch and attempt to connect n number of times again in the catch block.