Consuming a Webservice from behind a Proxy Firewall


I was trying to access a webservice from my office which is behind a proxy firewall, I was annoyed by seeing the following error message

"- The request failed with HTTP status 407: Proxy Authentication Required"

I knew that it is something to do with the firewall proxy. So I googled my doubt and the error text but couldn't get a descent solution. Then I tried with the options in the command WSDL that we use to generate a proxy class (not to be confused with firewall proxy) for our webservice. I found the following options.

 /proxy:

 /proxyusername:

 /proxypassword:

 /proxydomain:

But I didn't know how to use these options (I should admit that I am not good at DOS). Then I googled these options and got some answers which I am going to share with you now. 

  • /proxy: is the url of the proxy server with the port number. For E.g., if your proxy server address is 255.255.255.255 and the port is 8001 then your proxy url is http://255.255.255.255:8001 
  • /proxyusername: is the username of your proxy server and /proxypassword: is the password of your proxy server. 
  • /proxydomain: is the domain name of your server. So the WSDL command should be something like this.

c:\WSDL /proxy:http://255.255.255.255:8001 /proxyusername:uname /proxypassword:pwd
/proxydomain:lotus http://thiagu007.tk.105.webhostforasp.net/service1.asmx?wsdl
 

The above command will download the proxy class file for the webservice. But this is not the end of the show. We need to consume the service. The first thing we need to do is compile the class to a dll.

c:\csc.exe \t:library service1.cs 

Then create a windows application project in Visual Studio and add the dll as a reference. Now you can use the class that is available in the dll and create an instance of it. But calling the web method from the webservice will again display the same error. To resolve this you have to create a WebProxy. This is again the authenication information for firewall proxy.

To instantiate the WebProxy class you need to include the namespace System.Net in the "using" part. using System.Net; Now you can instantiate WebProxy class as follows,

// Instantiate the WebProxy class
WebProxy myProxy = new WebProxy(http://255.255.255.255:8001,true);
myProxy.Credentials = new NetworkCredential("uname", "pwd", "lotus");

Now, the proxy should be assigned to the Service class and the function can be called. 

// Instantiate the WebService class
Service1 sr= new Service1();
sr.Proxy = myProxy; // Set the proxy to the class
String str = sr.HelloWorld(); // Calling the web method

This will call the method that is available in the webservice by authenticating the proxy server's credentials.

Happy Coding :)


Similar Articles