Thursday, October 20, 2005

.NET : Web services behind firewall

If you are trying to generate the web service proxy classes by using "Add Web Reference" from Visual Studio 2003 and the URL you are trying to get to is behind a firewall, you are screwed! Big time!

There's no way to do it.

You need to run Visual Studio command line (See earlier post) and then run "wsdl.exe".

Just type "wsdl" and you'll get the options. You'll note that you can specify the firewall address and the user logon / password to get you through the firewall as options. Bingo!

e.g. wsdl http://URL.wsdl /proxy:xx.xx.xx.xx /pu:User /pp:Password

So now you can go ahead and generate the proxy stubs. Add this file to your project and you are away.

But wait - there's more!

When you try and run the program to actually access the web service behind the firewall you will have exactly the same problem - the dreaded "407 Proxy Authentication Required ( The ISA Server requires authorization to fulfill the request. Access to the Web Proxy service is denied. )"

The way around this is to programmatically specify the proxy / logon / password.

Assume that you have the proxy configured in IE under "Tools / Options / Connections / LAN Settings". (Note that this does NOT allow user logon / password to be specified).

(See also :
The proxy settings on this computer are not configured correctly for Web discovery).

You create the webservice from the proxy class in the normal way.

using System.Net;

...

wsName = new WebServiceName ();

// Use default IE setting as above.
wsName.Proxy = System.Net.WebProxy.GetDefaultProxy();
// Set credentials
wsName.Proxy.Credentials = new NetworkCredential("User", "Password");

Enjoy!

No comments: