【问题标题】:What is the relationship between WebProxy & IWebProxy with respect to WebClient?WebProxy 和 IWebProxy 与 WebClient 有什么关系?
【发布时间】:2010-05-21 23:42:02
【问题描述】:

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

【问题讨论】:

    标签: c# .net webclient webproxy


    【解决方案1】:

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

    根据WebClient page on MSDN...

    备注

    Proxy 属性标识 进行通信的 IWebProxy 实例 代表此与远程服务器 WebClient 对象。代理由 系统使用配置文件 和 Internet Explorer 本地区域 网络设置。指定没有 应该使用代理,设置代理 代理实例的属性 由 GetEmptyWebProxy 返回 方法。

    有关自动代理的信息 检测,见自动代理 检测。

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

    【讨论】:

      猜你喜欢
      • 2011-10-12
      • 1970-01-01
      • 2019-09-14
      • 2011-08-19
      • 2018-04-20
      • 2013-08-21
      • 1970-01-01
      • 1970-01-01
      • 2011-11-17
      相关资源
      最近更新 更多