Http Client Protocol Issues

If you use any of these solutions, Please let me know so I can track if any of this is useful to you! Thanks! This is an area to share observations I have made working with Http Client Protocols and the associated technologies. I currently work for the Microsoft team that supports the WinInet, WinHTTP and System.Net API's and classes associated with these technologies. This is not a replacement for Microsoft Support, but an area to discuss these technologies. These postings are provided "AS IS" with no warranties, and confer no rights. Use of included code samples are subject to the terms specified at Microsoft - Information on Terms of Use

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 "http://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="http://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:
http://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: http://msdn.microsoft.com/en-us/library/kd3cf2ex(VS.80).aspx, http://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 http://www.microsoft.com/sysinternals

Leave me a message if you were able to solve a problem with this article!

 

Published Wednesday, June 03, 2009 1:31 PM by jpsanders
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

No Comments

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required
Submit

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker