简体   繁体   中英

Consuming a Webservice from behind a Proxy

Yes, another question on how to: Consume as Web Service from behind a Proxy.

Ok so I know this quesiton has been answered elsewhere on this forum and elsewhere on the net. But for some reason my setup is not working. So here is my situation:

I am using Visual Studio 2010 Express to connect to a Web Service, via a WSDL-generated .CS file (not using Web Reference).

When I try to invoke a simple Ping() function (as shown below) I get the following error:

WebException was unhanlded: The request failed with HTTP status 407: Proxy Authentication Required.

The code I am using is as shown below. I do not understand why I am getting this error. I am using the same username and password I supply to the Web Browser when it prompts me for credentials for the proxy.

Any advice would be greatly appreciated!!!!

Thanks in advance.

            PingResponseDocument theResponse;

            WebProxy wp = new WebProxy("IP_ADDRESS:PORT_NO", true);
            wp.Credentials = new NetworkCredential("USER_NAME", "PASSWORD", "IP_ADDRESS:PORT_NO");

            WebService test = new WebService();
            PingRequestDocument doc = new PingRequestDocument();

            test.Proxy = wp;
            theResponse = test.ping(doc);

Please try this.

  1. Press Ctrl + Shift + A and create a new Application Configuration File if you already havnt added yet.
  2. Now add system.net element in your app.config as shown below.

     <?xml version="1.0" encoding="utf-8" ?> <configuration> <system.net> <defaultProxy useDefaultCredentials="true" /> </system.net> </configuration> 

UPDATE:

So I have found the answer to my question. Basically when you need to consume a Web Service you should always check to see if it requires a Proxy. You do this by using the following function:

isbypassed()

If this returns true, then you don't need Proxy Credentials and you can use the following:

WebProxy p = new WebProxy();
WebService w = new WebService(); // Your Web Service.
p.BypassList = new string[] {"IP_ADDRESS:PORT_NUMBER/dir../dir"};
w.Proxy = p;

w.someFunctionofYours(); // the function you call.

Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM