An ASP.NET Blog
I work for Microsoft and help people and businesses make better use of technolgy to realize their full potential. The opinions mentioned herein are solely mine and do not reflect those of my employer.

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

Monday, August 29, 2005 9:23 AM
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 !!!

Feedback

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

Thank you, it works 9/20/2005 9:04 PM | RAjadurai

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

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? 10/13/2005 12:21 AM | Frank

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

Thanks a lot. It worked 10/18/2005 8:42 AM | Vinod

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

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. 10/20/2005 12:33 AM | Kyle Fu

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

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. 10/20/2005 6:37 AM | Kyle Fu

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

thanks, but I need to consume a WS that is behind a firewall..any clue? 2/11/2006 11:52 PM | nevermind

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

Thanks a lot for ur code. 2/18/2006 7:50 AM | Nayeem

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

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. 4/4/2006 1:19 PM | Martin Kinyua

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

Do you know how to bring the firewall down w.r.t. Windows 2000 advanced server? 4/24/2006 7:29 AM | Sandhya

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

Thanks, I got the solution for my problem. 5/3/2006 5:02 AM | Chandra

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

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? 5/18/2006 3:21 PM | June

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

GREAT!!!! That was a tricky problem and your suggestion was absolutely correct! That ####### proxy!!! THANKS! 6/14/2006 8:05 AM | Malo Supremo

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

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? 6/14/2006 8:31 AM | Shantanu

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

yes i received the mesage but my system not behiend in firewall how can resolve the problem?
8/17/2006 3:04 AM | Harish

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

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). 11/1/2006 3:45 PM | David Sterling

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

Many thanks. It is working.. 11/8/2006 8:43 AM | Dhanasekaran

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

I've been trying the different solutions but still i'm facing problems, and it's giving me errors.
1/8/2007 6:22 AM | Sayed Majid

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

How to get my Proxy address? 2/25/2007 5:08 PM | Ira

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

following link may be useful

http://nilangshah.wordpress.com/2007/03/01/the-underlying-connection-was-closed-unable-to-connect-to-the-remote-server/ 3/1/2007 12:04 PM | Nilang Shah

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

Slap that fool 7/4/2007 12:46 AM | koos

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

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. 2/10/2008 9:25 PM | Rod Fornillos IV

Post a comment





 

Please add 6 and 2 and type the answer here: