我正在创建一个应用程序(.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 实例是否正在使用代理信息?