pavanred
pavanred

Reputation: 13793

Unable to connect to a web service

I am able to access the web service from the web browser but on the same computer if I try to connect to the web service through code I get a The remote server returned an error: (407) Proxy Authentication Required. error.

I have supplied all the credentials required. Here is my code -

MyWebService objWS = new MyWebService();  // my Web service object

// My credentials
System.Net.NetworkCredential cr = new System.Net.NetworkCredential(MyDomainUserName, UserPassword, DomainController);   

System.Net.WebProxy pr = new System.Net.WebProxy(ProxyServer, ProxyPort);

pr.Credentials = cr;   //Using my credentials for the local proxy

// Web Service credentials
System.Net.NetworkCredential cr1 = new System.Net.NetworkCredential("My_WebService_UserName", "My_WebService_Pwd");  

objWS.Credentials = cr1;   // Using WebService credentials to the WS object

//objWS.Credentials = CredentialCache.DefaultCredentials;  // I even tried Using default credentials but this didn't help.

objWS.Proxy = pr;   

Object result = objWS.WebMethod1(param1,param2,param3);

But, this throws an error - The remote server returned an error: (407) Proxy Authentication Required.

Any idea, what am I doing wrong here? Thanks.

Upvotes: 1

Views: 4893

Answers (3)

Bon
Bon

Reputation: 1091

You need to update your config file like this to fix this problem.

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

It isn't clear in this blog post, but this isn't either/or. Both things are required to fix your problem.

http://blogs.msdn.com/b/jpsanders/archive/2009/03/24/httpwebrequest-webexcepton-the-remote-server-returned-an-error-407-proxy-authentication-required.aspx

Upvotes: 3

Karthik Ramachandran
Karthik Ramachandran

Reputation: 12175

This means that your proxy needs some credentials(user name and pasword.) Figure out what they are and set them on pry.

Upvotes: 0

Edwin de Koning
Edwin de Koning

Reputation: 14387

You get this error because you are trying to access the service from behind a proxy. Try to use this code (you probably only need the last line):

//Set the system proxy with valid server address or IP and port, for example.
System.Net.WebProxy pry = new System.Net.WebProxy("172.16.0.1",8080);
//The DefaultCredentials should be enough.
pry.Credentials = CredentialCache.DefaultCredentials;
GlobalProxySelection.Select = pry;

Upvotes: 1

Related Questions