无法连接到Web服务

时间:2011-02-01 07:17:41

标签: c# web-services authentication proxy

我可以从网络浏览器访问网络服务但是在同一台计算机上如果我尝试通过代码连接到网络服务,我会收到The remote server returned an error: (407) Proxy Authentication Required.错误。

我提供了所需的所有凭据。这是我的代码 -

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);

但是,这会引发错误 - The remote server returned an error: (407) Proxy Authentication Required.

任何想法,我在这里做错了什么?感谢。

3 个答案:

答案 0 :(得分:3)

您需要像这样更新配置文件以解决此问题。

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

在这篇博文中并不清楚,但这不是/或。这两件事都需要解决你的问题。

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

答案 1 :(得分:1)

您收到此错误是因为您尝试从代理后面访问该服务。 尝试使用此代码(您可能只需要最后一行):

//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;

答案 2 :(得分:0)

这意味着您的代理需要一些凭据(用户名和密码)。找出它们是什么并将它们设置为pry。