views:

918

answers:

5

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.

A: 

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

Jon
No. Webservice is a publicly available service on internet. Its not on same network.
Hemant
A: 

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.

Eric Petroelje
+3  A: 

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);
AaronS
+2  A: 

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.

Marc Gravell
Is it possible to do above through code? The reason is that the application that will need to access web service is a windows form application and that might run on a series of computers. It is not feasible to ask users to take above steps.
Hemant
+2  A: 

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.

Hemant
You were unable to do the above in the <defaultProxy/> configuration element?
John Saunders
I tried to use the code (not config file though) but it didn't work.Actually the application that I was intending to consume this service is a windows application. Not really sure it matters or not.
Hemant