You may receive the error: "The underlying connection was closed: Unable to connect to the remote server." while consuming a Webservice

You may receive the error "The underlying connection was closed: Unable to connect to the remote server." while trying to consume a Web Service from your ASP.NET Web Application. However, the same Web Service can be consumed from a Windows Application without any issues.

This error particularly occurs if you are behind a firewall or proxy. When you use a winforms app, it can autodetect the proxy using IE settings for current user, and it can connect out through the proxy. However, when in asp.net, it cannot because the asp.net user identitiy does not have the correct proxy settings.

To resolve this issue, you need to explicitly specify the proxy settings for your application. You can do it at various levels viz., at the Machine.Config file which will apply for all the applications running on the system, at the Web.Config file such that it applies for a single application, and at the Page Level programattically in the code behind / code inline.

Web.Config setting

<configuration>
<system.net>
<defaultProxy>
<proxy
usesystemdefault = "false"
proxyaddress="http://address:port"
bypassonlocal="false"
/>
</defaultProxy>
</system.net>
<system.web>


For Machine.Config use the same settings as above.

To do it programmatically,

using System.Net;

WebServiceClass MyWebServiceClass = new WebServiceClass();
WebProxy proxyObject = new WebProxy("http://address:port", true);
MyWebServiceClass.Proxy = proxyObject;
MyWebServiceClass.MyWebMethod();


where WebServiceClass is the name of the Web Service Class which you are consuming.

That should solve the issue with consuming the Web Service across Proxy Settings.

Cheers and Happy Web Servicing !!!

Print | posted on Monday, August 29, 2005 9:23 AM

Comments on this post

# re: You may receive the error: "The underlying connection was closed: Unable to connect to the remote server." while consuming a Webservice

Requesting Gravatar...
Thank you, it works
Left by RAjadurai on Sep 20, 2005 9:04 PM

# re: You may receive the error: "The underlying connection was closed: Unable to connect to the remote server." while consuming a Webservice

Requesting Gravatar...
Thanks for the post. I tried the code and the error happens later, when everything related to webservice finished, and the position of the error now is unidentified. Therefore I have no clue what happen.
Do you have any idea?
Left by Frank on Oct 13, 2005 12:21 AM

# re: You may receive the error: "The underlying connection was closed: Unable to connect to the remote server." while consuming a Webservice

Requesting Gravatar...
Thanks a lot. It worked
Left by Vinod on Oct 18, 2005 8:42 AM

# re: You may receive the error: "The underlying connection was closed: Unable to connect to the remote server." while consuming a Webservice

Requesting Gravatar...
But i have other problems. I tried to get local proxy and set it to my WebServices' proxy, but i used GetDefaultProxy() and failed. How can i do?
Thanks.
Left by Kyle Fu on Oct 20, 2005 12:33 AM

# re: You may receive the error: "The underlying connection was closed: Unable to connect to the remote server." while consuming a Webservice

Requesting Gravatar...
And in Windows Application i can get proxy by GetDefaultProxy(), now i want to get local PC's proxy in Web Application , How can i do?
Thanks.
Left by Kyle Fu on Oct 20, 2005 6:37 AM

# re: You may receive the error: "The underlying connection was closed: Unable to connect to the remote server." while consuming a Webservice

Requesting Gravatar...
thanks, but I need to consume a WS that is behind a firewall..any clue?
Left by nevermind on Feb 11, 2006 11:52 PM

# re: You may receive the error: "The underlying connection was closed: Unable to connect to the remote server." while consuming a Webservice

Requesting Gravatar...
Thanks a lot for ur code.
Left by Nayeem on Feb 18, 2006 7:50 AM

# re: You may receive the error: "The underlying connection was closed: Unable to connect to the remote server." while consuming a Webservice

Requesting Gravatar...
This works for me. The code should however not be placed within the System.web section of the code. I made that mistake and it did not work. However, when i placed it out, it worked.
Left by Martin Kinyua on Apr 04, 2006 1:19 PM

# re: You may receive the error: "The underlying connection was closed: Unable to connect to the remote server." while consuming a Webservice

Requesting Gravatar...
Do you know how to bring the firewall down w.r.t. Windows 2000 advanced server?
Left by Sandhya on Apr 24, 2006 7:29 AM

# re: You may receive the error: "The underlying connection was closed: Unable to connect to the remote server." while consuming a Webservice

Requesting Gravatar...
Thanks, I got the solution for my problem.
Left by Chandra on May 03, 2006 5:02 AM

# re: You may receive the error: "The underlying connection was closed: Unable to connect to the remote server." while consuming a Webservice

Requesting Gravatar...
I didn't have this problem until I changed routers is there a setting on the router to allow this traffic?
I have tried adding the code to my Web.config file but I can't get it to work. My webservice is hosted at www.swelp.com/taxServices and I am trying to access the service from www.swelp.com. What should the proxy address be?
Left by June on May 18, 2006 3:21 PM

# re: You may receive the error: "The underlying connection was closed: Unable to connect to the remote server." while consuming a Webservice

Requesting Gravatar...
GREAT!!!! That was a tricky problem and your suggestion was absolutely correct! That ####### proxy!!! THANKS!
Left by Malo Supremo on Jun 14, 2006 8:05 AM

# re: You may receive the error: "The underlying connection was closed: Unable to connect to the remote server." while consuming a Webservice

Requesting Gravatar...
When i tried with that it displayed the following error. "The ServicePointManager does not support proxies of https scheme."

can u tell me waht is this?
Left by Shantanu on Jun 14, 2006 8:31 AM

# re: You may receive the error: "The underlying connection was closed: Unable to connect to the remote server." while consuming a Webservice

Requesting Gravatar...
yes i received the mesage but my system not behiend in firewall how can resolve the problem?
Left by Harish on Aug 17, 2006 3:04 AM

# re: You may receive the error: "The underlying connection was closed: Unable to connect to the remote server." while consuming a Webservice

Requesting Gravatar...
Outstanding; happened upon this problem with a Virtual Server at a client site. FYI: You must add an entry for every port (in my case, 3 SharePoint sites).
Left by David Sterling on Nov 01, 2006 3:45 PM

# re: You may receive the error: "The underlying connection was closed: Unable to connect to the remote server." while consuming a Webservice

Requesting Gravatar...
Many thanks. It is working..
Left by Dhanasekaran on Nov 08, 2006 8:43 AM

# re: You may receive the error: "The underlying connection was closed: Unable to connect to the remote server." while consuming a Webservice

Requesting Gravatar...
I've been trying the different solutions but still i'm facing problems, and it's giving me errors.
Left by Sayed Majid on Jan 08, 2007 6:22 AM

# re: You may receive the error: "The underlying connection was closed: Unable to connect to the remote server." while consuming a Webservice

Requesting Gravatar...
How to get my Proxy address?
Left by Ira on Feb 25, 2007 5:08 PM

# re: You may receive the error: "The underlying connection was closed: Unable to connect to the remote server." while consuming a Webservice

Left by Nilang Shah on Mar 01, 2007 12:04 PM

# re: You may receive the error: "The underlying connection was closed: Unable to connect to the remote server." while consuming a

Requesting Gravatar...
Slap that fool
Left by koos on Jul 04, 2007 12:46 AM

# re: You may receive the error: "The underlying connection was closed: Unable to connect to the remote server." while consuming a

Requesting Gravatar...
thanks for the information. m still having a problem though... i've created a web service in asp.net 2.0. and deployed it in one of our remote server.

i've developed a simple application that will consume the service, in my local machine, and it works. however, when i tried to use the web service in another application (let's call this "web application b"), i get that error.

basically, "web application b" is deployed in that same remote server as the web service, only a different virtual folder.

i'm not sure if this will help, but the remote server hosts 3 websites (sharing the same IP), with each one having their own host header. so we can say that both "web application b" and "web service" are running on say "Website A", only with their own respective virtual directory.

hope you can help.
Left by Rod Fornillos IV on Feb 10, 2008 9:25 PM

Your comment:

 (will show your gravatar)