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 !!!

posted @ Monday, August 29, 2005 9:23 AM

Print

Comments on this entry:

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

Left by RAjadurai at 9/20/2005 9:04 PM
Gravatar
Thank you, it works

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

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

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

Left by Vinod at 10/18/2005 8:42 AM
Gravatar
Thanks a lot. It worked

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

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

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

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

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

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

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

Left by Nayeem at 2/18/2006 7:50 AM
Gravatar
Thanks a lot for ur code.

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

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

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

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

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

Left by Chandra at 5/3/2006 5:02 AM
Gravatar
Thanks, I got the solution for my problem.

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

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

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

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

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

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

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

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

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

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

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

Left by Dhanasekaran at 11/8/2006 8:43 AM
Gravatar
Many thanks. It is working..

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

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

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

Left by Ira at 2/25/2007 5:08 PM
Gravatar
How to get my Proxy address?

# 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 at 3/1/2007 12:04 PM
Gravatar

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

Left by koos at 7/4/2007 12:46 AM
Gravatar
Slap that fool

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

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

Your comment:



 (will not be displayed)


 
 
 
 
 

Live Comment Preview:

 
«November»
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345