【问题标题】:C# webclient and proxy serverC# webclient 和代理服务器
【发布时间】:2010-10-23 11:38:23
【问题描述】:

我在我的源代码中使用 Web 客户端类来使用 http 下载字符串。

这工作正常。但是,公司中的客户端现在都连接到代理服务器。而问题就是从这里开始的。

当我测试我的应用程序时,我认为它不能通过代理服务器,因为不断抛出的异常是“没有来自代理服务器 IP 地址的 xxx.xxx.xxx.xxx 的响应。

但是,我仍然可以导航到网站 URL,并且当通过代理服务器连接时,它会在浏览器中正确显示字符串,但在我使用 Web 客户端时却不能。

Web 客户端中是否有一些我必须配置以允许我从代理服务器后面访问 url 的东西?

using (WebClient wc = new WebClient())
{
    string strURL = "http://xxxxxxxxxxxxxxxxxxxxxxxx";

    //Download only when the webclient is not busy.
    if (!wc.IsBusy)
    {
        string rtn_msg = string.Empty;
        try
        {
            rtn_msg = wc.DownloadString(new Uri(strURL));
            return rtn_msg;
        }
        catch (WebException ex)
        {
            Console.Write(ex.Message);
            return false;
        }
        catch (Exception ex)
        {
            Console.Write(ex.Message);
            return false;
        }
    }
    else
    {
        System.Windows.Forms.MessageBox.Show("Busy please try again");
        return false;
    }
}

【问题讨论】:

    标签: c# proxy webclient


    【解决方案1】:

    我的解决方案:

    WebClient client = new WebClient();
    WebProxy wp = new WebProxy(" proxy server url here");
    client.Proxy = wp;
    string str = client.DownloadString("http://www.google.com");
    

    【讨论】:

    • 理想情况下,WebClient 也应该在 using 语句中,因为它实现了 IDisposable。不过有用的帖子。
    • 比Orphid更朴素;使用上面的代码(完全合法)不要忘记在网络客户端上调用.Dispose!此外,WebProxy 有一个构造函数重载,它将端口号作为第二个参数
    【解决方案2】:

    如果需要对代理进行认证,需要将UseDefaultCredentials设置为false,并设置代理Credentials

    WebProxy proxy = new WebProxy();
    proxy.Address = new Uri("mywebproxyserver.com");
    proxy.Credentials = new NetworkCredential("usernameHere", "pa****rdHere");  //These can be replaced by user input
    proxy.UseDefaultCredentials = false;
    proxy.BypassProxyOnLocal = false;  //still use the proxy for local addresses
    
    WebClient client = new WebClient();
    client.Proxy = proxy;
    
    string doc = client.DownloadString("http://www.google.com/");
    

    如果你只需要一个简单的代理,你可以跳过上面的大部分行。您只需要:

    WebProxy proxy = new WebProxy("mywebproxyserver.com");
    

    【讨论】:

      【解决方案3】:

      Jonathan 提出的答案是正确的,但要求您在代码中指定代理凭据和 url。通常,默认情况下最好允许在系统中使用凭据作为设置(用户通常会配置 LAN 设置以防万一他们使用代理)...

      Davide 在较早的答案中提供了以下答案,但这需要修改 app.config 文件。这个解决方案可能更有用,因为它在代码中做同样的事情。

      为了让应用程序使用用户系统中使用的默认代理设置,可以使用以下代码:

      IWebProxy wp = WebRequest.DefaultWebProxy;
      wp.Credentials = CredentialCache.DefaultCredentials; 
      wc.Proxy = wp;
      

      这将允许应用程序代码使用代理(使用登录凭据和默认代理 url 设置)...不用头疼! :)

      希望这有助于该页面的未来查看者解决他们的问题!

      【讨论】:

      • 为什么我在运行 Fiddler(本地代理客户端)时不必这样做?使用 WebClient 时,所有调用都通过 fiddler 进行通道,即使没有配置代理。
      【解决方案4】:

      我也遇到过同样的问题,但我使用 webclient 从 Internet 下载文件,并使用 Winform 应用程序将解决方案添加到 app.config 中:

      <system.net>
          <defaultProxy useDefaultCredentials="true" />
      </system.net>
      

      同样的解决方案适用于在 web.config 中插入相同行的 asp.net 应用程序。

      希望它会有所帮助。

      【讨论】:

        【解决方案5】:

        您需要在 WebClient 对象中配置代理。

        查看 WebClient.Proxy 属性:

        http://msdn.microsoft.com/en-us/library/system.net.webclient.proxy(VS.80).aspx

        【讨论】:

          【解决方案6】:
          byte[] data;
          using (WebClient client = new WebClient())
          {
              ICredentials cred;
              cred = new NetworkCredential("xmen@test.com", "mybestpassword");
              client.Proxy = new WebProxy("192.168.0.1",8000);
              client.Credentials = cred;
              string myurl="http://mytestsite.com/source.jpg";
              data = client.DownloadData(myUrl);
          }
          
          File.WriteAllBytes(@"c:\images\target.jpg", data);
          

          【讨论】:

            【解决方案7】:

            之前所有的答案都有一些优点,但实际答案只需要一行:

            wc.Proxy = new WebProxy("127.0.0.1", 8888);
            

            其中 wc 是 WebClient 对象,8888 是位于同一台机器上的代理服务器的端口号。

            【讨论】: