add web service reference from behind a proxy server

Nikita picture Nikita · Sep 12, 2011 · Viewed 38.2k times · Source

I'm using vs2010. In a simple console app I try to add a service reference to http://***/service1.asmx , old asmx service. My computer is behind a proxy server, so i get an error :

"The remote server returned an unexpected response: (407) Proxy Authentication Required."

When im using wsdl tool i can not define proxy server port number and i get message that server, for examle 10.0.0.3:80, did not respond, but i need to specify 8080 port and don't know how. How could i create a reference?

Answer

Israel Margulies picture Israel Margulies · Nov 6, 2012

I spent almost 50 hours finding the problem, could not find anywhere on the web this simple solution.

Under "configuration" section in Web.config add this:

  <system.net>
    <defaultProxy enabled="true" useDefaultCredentials="true"></defaultProxy>
</system.net>

Then works like a charm!

You can also do it from the code behind:

serviceConnection = new WebService1();
serviceConnection.Proxy = System.Net.HttpWebRequest.GetSystemWebProxy();
serviceConnection.Proxy.Credentials = CredentialCache.DefaultCredentials; 

Works beautiful!!.

If you need to consume from HTTPS location add this configuration:

<message clientCredentialType="Certificate" algorithmSuite="Default" />