[ACCEPTED]-Calling a webservice from behind a proxy server-soap

Accepted answer
Score: 11

It will use port 80 by default, and you 5 shouldn't have to do any further coding.

If 4 you do need to go through a proxy of some 3 sort, all you need to do is add the following 2 to your web.config:

  <system.net>
    <defaultProxy>
      <proxy  proxyaddress="http://yourproxyserver:80" />
    </defaultProxy>
  </system.net>

You could also do it 1 through code using this:

WebRequest.DefaultWebProxy = new WebProxy("http://yourproxyserver:80/",true);
Score: 8

You can use the default setting from you 2 local machine:

System.Net.ServicePointManager.Expect100Continue = false; 
wsclient.Proxy=  System.Net.HttpWebRequest.GetSystemWebProxy();
wsclient.Proxy.Credentials = CredentialCache.DefaultCredentials;     

and in app.config add this 1 configuration:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.net>
    <settings>
      <servicePointManager expect100Continue="false" />
    </settings>
  </system.net>
</configuration>
Score: 7

OK. So I did some experiments and it turns 9 out that we do need to write some code to 8 make it work from behind the proxy server. (Though 7 I would have prefered a better solution)

So 6 it actually drills down to asking proxy 5 server details from user and then configure 4 the service proxy class for proxy server 3 as below:

var networkCredentials = new NetworkCredential ("username", "password", "domain");
WebProxy myProxy = new WebProxy ("W.X.Y.Z:NN", true) {Credentials = networkCredentials};
var service = new iptocountry { Proxy = myProxy };
string result = service.FindCountryAsString ("A.B.C.D");

I wrote a test class and it uses 2 IP To Country free web service.

Using above code, I could 1 consume the web service successfully.

Score: 5

The inbuilt code (WebClient, WCF, HttpWebRequest, etc) all 10 make use of the WinHTTP configuration to 9 obtain proxy configuration. So all you need 8 to do is configure WinHTTP to know about 7 the proxy!

In XP, this is:

proxycfg -u

which imports the 6 settings from the user's IE proxy settings 5 (WinInet).

On Vista / etc, you use

netsh winhttp

(and some 4 subcommand like "import")

untested, but try:

netsh winhttp import proxy source=ie

After 3 that, your .NET code should all work via 2 the proxy that the uses has presumably already 1 configured in order to use IE etc.

More Related questions