Get Proxy configuration before accessing an external webservice (.NET 2.0)

本秂侑毒 提交于 2019-12-21 21:40:44

问题


When trying to invoke a method on an external webservice (over the Internet) it throws me "The remote server returned an error: (407) Proxy Authentication Required."

To solve this, I used the following code to set the proxy we use in the office:

//Set the system proxy with valid server address or IP and port.
System.Net.WebProxy pry = new System.Net.WebProxy("MyHost", 8080);

//The DefaultCredentials automically get username and password.
pry.Credentials = System.Net.CredentialCache.DefaultCredentials;
System.Net.WebRequest.DefaultWebProxy = pry;

That works fine, but now... I need to do that "less harcoded" trying to get the information from my system instead of setting that manually.


回答1:


This will use whatever the default proxy is for IE I believe (not deprecated):

Services.MyService service = new Services.MyService();
service.UseDefaultCredentials = true;
service.Proxy = new System.Net.WebProxy();
service.Proxy.Credentials = service.Credentials;



回答2:


System.Net.WebProxy.GetDefaultProxy() although VS cautions its been deprecated.



来源:https://stackoverflow.com/questions/284511/get-proxy-configuration-before-accessing-an-external-webservice-net-2-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!