开发者

What is the relationship between WebProxy & IWebProxy with respect to WebClient?

开发者 https://www.devze.com 2023-01-01 03:42 出处:网络
I am creating an app (.NET 2.0) that uses WebClient to connect (downloaddata, etc) to/from a http web service.

I am creating an app (.NET 2.0) that uses WebClient to connect (downloaddata, etc) to/from a http web service. I am adding a form now to handle allowing proxy information to either be stored or set to use the defaults. I am a little confused about some things.

First, some of the methods & properties available in either WebProxy or IWebProxy are not in both. What is the difference here with respect to setting up how WebClient will be have when it is called?

Secondly, do I have to tell WebClient to use the proxy information if I set it using either WebProxy or IWebProxy class elsewhere? Or is it automatically inherited?

Thirdly, when giving the option for the user to use the default proxy (whatever is set in IE) and using the default credentials (I assume also whatever is set in IE) are these two mutually exclusive? Or you only use default credentials when you have also used default proxy?

This gets me to the whole difference between WebProxy and IWebProxy. WebRequest.DefaultProxy is a IWebPRoxy class but UseDefaultCredentials is not a method on the IWebProxy class, rather it is only on WebProxy and in turn, How to set the proxy to the WebRequest.DefautlProxy if they are two different classes?

Here is my current method to read the stored form settings by the user - but I am not sure if this is correct, not enough, overkill, or just wrong because of the mix of WebProxy and IWebProxy:

    private WebProxy _proxyInfo = new WebProxy();
    private WebProxy SetProxyInfo()
    {
        if (UseProxy) 
        {
            if (UseIEProxy)
            {
                // is doing this enough to set this as default for WebClient?
                IWebProxy iProxy = WebRequest.DefaultWebProxy; 
                if (UseIEProxyCredentials)
                {
                    _proxyInfo.UseDefaultCredentials = true;
                }
                else
                {
                    // is doing this enough to set this as default credentials for WebClient?
                    WebRequest.DefaultWebProxy.Credentials = new NetworkCredential(ProxyUsername, ProxyPassword);
                }
            }
            else
            {
                // is doing this enough to set this as default for WebClient?
                WebRequest.DefaultWebProxy = new WebProxy(ProxyAddress, ParseLib.StringToInt(ProxyPort));
                if (UseIEProxyCredentials)
                {
                    _proxyInfo.UseDefaultCredentials = true;
                }
                else
                {
                    WebRequest.DefaultWebProxy.Credentials = new NetworkCredential(ProxyUsername, ProxyPassword);
                }
            }
        }
        // Do I need to WebClient to absorb this returned proxy info if I didn't set or use defaults?
        return _proxyInfo;
    }

Is there any reason to not just scrap storing app specific proxy information and only allow the app the ability to use the default proxy information & credentials for the logged in user? Will this ever not be enough if using HTTP?

P开发者_JAVA技巧art 2 Question:

How can I test that the WebClient instance is using the proxy information or not?


IWebProxy is an interface, and WebProxy implements that interface. So, WebProxy can have more methods/properties that are not present on IWebProxy.

According to the WebClient page on MSDN...

Remarks

The Proxy property identifies the IWebProxy instance that communicates with remote servers on behalf of this WebClient object. The proxy is set by the system using configuration files and the Internet Explorer Local Area Network settings. To specify that no proxy should be used, set the Proxy property to the proxy instance returned by the GetEmptyWebProxy method.

For information on automatic proxy detection, see Automatic Proxy Detection.

Therefore, it should be ok if you do not explicitly specify a webproxy. It should use the one that IE is using (if any).

0

精彩评论

暂无评论...
验证码 换一张
取 消