2

我正在创建一个应用程序(.NET 2.0),它使用 WebClient 连接(下载数据等)到/从 http Web 服务。我现在添加一个表单来处理允许存储代理信息或设置为使用默认值。我对一些事情有点困惑。

首先,WebProxy 或 IWebProxy 中可用的一些方法和属性不在两者中。关于设置 WebClient 被调用时的方式,这里有什么区别?

其次,如果我在其他地方使用 WebProxy 或 IWebProxy 类设置它,我是否必须告诉 WebClient 使用代理信息?还是自动继承?

第三,当让用户选择使用默认代理(无论在 IE 中设置什么)和使用默认凭据(我也假设在 IE 中设置什么)这两者是互斥的吗?或者当您还使用默认代理时,您只使用默认凭据?

这让我了解了 WebProxy 和 IWebProxy 之间的全部区别。WebRequest.DefaultProxy 是 IWebPRoxy 类,但 UseDefaultCredentials 不是 IWebProxy 类上的方法,而是仅在 WebProxy 上,如果它们是两个不同的类,如何将代理设置为 WebRequest.DefautlProxy?

这是我当前读取用户存储的表单设置的方法 - 但由于 WebProxy 和 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;
    }

是否有任何理由不只是放弃存储应用程序特定的代理信息,而只允许应用程序能够为登录用户使用默认代理信息和凭据?如果使用 HTTP,这还不够吗?

第 2 部分问题:
如何测试 WebClient 实例是否正在使用代理信息?

4

1 回答 1

1

IWebProxy 是一个接口,而 WebProxy 实现了该接口。因此,WebProxy 可以拥有更多 IWebProxy 上不存在的方法/属性。

根据MSDN 上的 WebClient 页面...

评论

Proxy 属性标识代表此 WebClient 对象与远程服务器通信的 IWebProxy 实例。代理由系统使用配置文件和 Internet Explorer 局域网设置进行设置。要指定不应使用代理,请将 Proxy 属性设置为 GetEmptyWebProxy 方法返回的代理实例。

有关自动代理检测的信息,请参阅自动代理检测。

因此,如果您不明确指定 webproxy 应该没问题。它应该使用 IE 正在使用的那个(如果有的话)。

于 2010-05-23T23:06:53.233 回答