What's the difference between WebRequest.Defau

2020-05-23 07:30发布

问题:

I am wondering what the main differences are between DefaultWebProxy and GetSystemWebProxy(). There're some descriptions on MSDN, but I still feel I need a bit more details to have a better understanding.

Also, let's say I have following options for proxy configuration on my C# winform application

  1. Auto-detect proxy settings
  2. Use system default settings
  3. No proxy

Then which method goes to which option? Is it right to say that Auto-detect proxy somewhat equals Use system default settings?

回答1:

Per the MSDN article for WebRequest.DefaultWebProxy, this property will provide the proxy information specified in the app.config file. It looks like the .NET Framework v3.5 MSDN Article is missing this specific detail.

As far as WebRequest.GetSystemWebProxy() goes, the MSDN article for it states that this method will provide the system-wide configured proxy (Control Panel > Internet Options).


This is how I would suggest you implement the three options outlined:

  1. Auto-detect proxy settings
    • Implement your own proxy discovery logic; or
    • Change the system setting to match your application's setting, and use WebRequest.GetSystemWebProxy() (I wouldn't recommend this approach); or
    • Preferably not include this option, and let the user use the corresponding system-wide setting along with the "Use system default settings" option;
  2. Use system default settings
    • Use WebRequest.GetSystemWebProxy();
  3. No proxy
    • Ensure to unset the WebRequest.DefaultWebProxy and the WebRequest.Proxy properties;

Edit: If no proxy is configured in app.config WebRequest.DefaultWebRequest is almost the same as WebRequest.GetSystemWebProxy() (at least for .NET 4.5). The difference is that WebRequest.GetSystemWebProxy() will run the PAC script (if any) for proxy definition.
Thanks to Gabrielius and to 23W for the comments below.