Web Service call results in Exception: A socket operation was attempted to an unreachable host
There can be many reasons for this error. In some cases this error can be confusing. I have seen a few instances where an ASP.Net webservice call resulted in this error. More perplexing is the fact that Internet Explorer on the same machine can reach the WebService fine. If a firewall was blocking the traffic, most likely the Internet Explorer request to the same port would also be blocked. Also to add to the confusion is when adding the usesystemdefault=true (default setting) did the proxy not get used. The reason that usesystemdefault did not get the proxy is that IE stores the hard coded proxy information in the registry of the logged on user. When ASP.Net runs, it is not running in the context of the currently logged on user (normally) and so it cannot read the same information that IE is reading to connect to the proxy.
Some other errors around this exeption include: WebException: Unable to connect to the remote server, SocketException (0x2751): A socket operation was attempted to an unreachable host, Socket operation encountered a dead network
Typical call stacks look something like this.
Exception Details: System.Net.Sockets.SocketException: A socket operation was attempted to an unreachable host
[SocketException (0x2751): A socket operation was attempted to an unreachable host <<ipaddr:port>>]
System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) +1073657
System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP) +33
System.Net.ServicePoint.ConnectSocketInternal
WebException: Unable to connect to the remote server
System.Net.HttpWebRequest.GetRequestStream() +1534317
System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke
Each time, these issues were related to proxy settings. IE was able to get to the WebService but .NET could not. The reason is, the Proxy information was manually configured for IE.
A simple change to the Web.Config fixes the issue:
You will need to change "https://192.168.1.10:3128" to be the address of your proxy server. You should be able to determine the proxy from your Internet Explorer Settings.
<configuration>
<system.net>
<defaultProxy>
<proxy
proxyaddress="https://192.168.1.10:3128"
bypassonlocal="true"
/>
</defaultProxy>
</system.net>
</configuration>
Here is an article on how proxy detection works in the 2.0 framework:
https://msdn.microsoft.com/en-us/magazine/cc300743.aspx
You may also now need to provide credentials to get through the proxy. If the call does not go through and the exception indicates an HTTP 407 error, you can add the credentials to your config as well. Here are articles on the <system.net> schema and proxy setting: https://msdn.microsoft.com/en-us/library/kd3cf2ex(VS.80).aspx, https://msdn.microsoft.com/en-us/library/sa91de1e(VS.80).aspx
If the proxy settings are not the issue, ProcMon may help you identify any permissions related issues on the server. You can download this tool from https://www.microsoft.com/sysinternals
Leave me a message if you were able to solve a problem with this article!