Get Proxy configuration before accessing an extern

2019-04-14 09:48发布

问题:

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.



标签: proxy c#-2.0