38

我在源代码中使用 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;
    }
}
4

7 回答 7

50

我的解决方案:

WebClient client = new WebClient();
WebProxy wp = new WebProxy(" proxy server url here");
client.Proxy = wp;
string str = client.DownloadString("http://www.google.com");
于 2012-05-28T04:01:13.190 回答
20

如果需要对代理进行身份验证,则需要设置UseDefaultCredentialsfalse,并设置代理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");
于 2014-08-15T17:47:40.337 回答
11

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

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

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

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

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

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

于 2014-08-11T06:46:47.670 回答
10

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

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

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

希望它会有所帮助。

于 2014-01-17T10:24:48.563 回答
6

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

请参阅 WebClient.Proxy 属性:

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

于 2009-05-03T17:33:18.910 回答
4
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);
于 2016-12-30T20:22:04.933 回答
0

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

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

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

于 2019-11-22T13:45:30.607 回答