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

Posted by Streamline on Stack Overflow See other posts from Stack Overflow or by Streamline
Published on 2010-05-21T23:42:02Z Indexed on 2010/05/21 23:51 UTC
Read the original article Hit count: 377

Filed under:
|
|
|

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?

Part 2 Question:
How can I test that the WebClient instance is using the proxy information or not?

© Stack Overflow or respective owner

Related posts about .NET

Related posts about c#