Hemant
Hemant

Reputation: 19846

Calling a webservice from behind a proxy server

I need to add a functionality in an application (C#) which will use a web service (XML SOAP service).

Now this application can (and mostly) be used in an corporate environment which has a proxy server in place.

I understand the SOAP services use HTTP protocol and hence should use port 80, which is normally kept opened. Is it right that application can use web service without any special coding or I will need to write special code to detect proxy settings or some other issues you see?

EDIT: Webservice is a publicly available service on internet. Its not on same network.

Upvotes: 9

Views: 52843

Answers (6)

kusnaditjung tjung
kusnaditjung tjung

Reputation: 339

You can use the default setting from you local machine:

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

and in app.config add this configuration:

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

Upvotes: 8

Hemant
Hemant

Reputation: 19846

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

So it actually drills down to asking proxy server details from user and then configure the service proxy class for proxy server 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 IP To Country free web service.

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

Upvotes: 7

Marc Gravell
Marc Gravell

Reputation: 1064004

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

In XP, this is:

proxycfg -u

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

On Vista / etc, you use

netsh winhttp

(and some subcommand like "import")

untested, but try:

netsh winhttp import proxy source=ie

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

Upvotes: 5

AaronS
AaronS

Reputation: 7713

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

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

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

You could also do it through code using this:

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

Upvotes: 12

Eric Petroelje
Eric Petroelje

Reputation: 60529

As long as web traffic (port 80) is allowed through, you shouldn't need to do anything special. From a router / proxy server's perspective web service calls are the same as any other HTTP traffic.

Upvotes: -1

jgallant
jgallant

Reputation: 11273

If your webservice is on the same internal network as the client calling the webservice, then it shouldn't be going through a proxy.

Upvotes: -1

Related Questions