简体   繁体   English

C# webclient 和代理服务器

[英]C# webclient and proxy server

I am using a web client class in my source code for downloading a string using http.我在源代码中使用 Web 客户端类来使用 http 下载字符串。

This was working fine.这工作正常。 However, the clients in the company are all connected now to a proxy server.但是,公司中的客户端现在都连接到代理服务器。 And the problem started from this.而问题由此开始。

When I have tested my application I don't think it can pass through the proxy server, as the exception that keeps getting thrown is "no response from xxx.xxx.xxx.xxx which is the proxy server IP address.当我测试我的应用程序时,我认为它不能通过代理服务器,因为不断抛出的异常是“来自代理服务器 IP 地址 xxx.xxx.xxx.xxx 没有响应。

However, I can still navigate to the web site URL and it displays the string correctly in the browser when connecting through a proxy server, but not when I use my web client.但是,我仍然可以导航到网站 URL,并且在通过代理服务器连接时它会在浏览器中正确显示字符串,但在我使用 Web 客户端时则不能。

Is there something in the web client that I have to configure to allow me to access the url from behind a proxy server?我必须配置 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;
    }
}

My solution:我的解决方案:

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

If you need to authenticate to the proxy, you need to set UseDefaultCredentials to false , and set the proxy Credentials .如果需要对代理进行身份验证,则需要将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/");

If all you need is a simple proxy, you skip most of the lines above though.如果您只需要一个简单的代理,那么您可以跳过上面的大部分内容。 All you need is:所有你需要的是:

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

The answer proposed by Jonathan is proper, but requires that you specify the proxy credentials and url in the code. Jonathan 提出的答案是正确的,但要求您在代码中指定代理凭据和 url。 Usually, it is better to allow usage of the credentials as setup in the system by default (Users typically configure LAN Settings anyway in case they use a proxy)...通常,默认情况下,最好允许使用凭据作为系统中的设置(用户通常无论如何都要配置 LAN 设置,以防他们使用代理)...

The below answer has been provided by Davide in earlier answer, but that requires modifying the app.config files. Davide 在之前的答案中提供了以下答案,但这需要修改 app.config 文件。 This solution is probably more useful since it does the same thing IN CODE.这个解决方案可能更有用,因为它在代码中做同样的事情。

In order to let the application use the default proxy settings as used in the user's system, one can use the following code:为了让应用程序使用用户系统中使用的默认代理设置,可以使用以下代码:

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

This will allow the application code to use the proxy (with logged-in credentials and default proxy url settings)... No headaches!这将允许应用程序代码使用代理(使用登录凭据和默认代理 url 设置)...没有麻烦! :) :)

Hope this helps future viewers of this page to solve their problem!希望这有助于此页面的未来观众解决他们的问题!

I've encountered the same issue but using a webclient for downloading a file from the internet with a Winform application the solution was adding in the app.config:我遇到了同样的问题,但使用 webclient 从 Internet 下载文件,其中包含 Winform 应用程序,该解决方案添加到 app.config 中:

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

The same solution will work for an asp.net app inserting the same rows in web.config.相同的解决方案适用于在 web.config 中插入相同行的 asp.net 应用程序。

Hope it will help.希望它会有所帮助。

You need to configure the proxy in the WebClient object.您需要在 WebClient 对象中配置代理。

See the WebClient.Proxy property:请参阅 WebClient.Proxy 属性:

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

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);

All previous answers have some merit, but the actual answer only needs ONE line:以前的所有答案都有一些优点,但实际答案只需要一行:

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

where wc is the WebClient object, and 8888 is the port number of the proxy server located on the same machine.其中 wc 是 WebClient 对象,8888 是位于同一台机器上的代理服务器的端口号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM