简体   繁体   English

将C#WebClient与代理一起使用 - 没有请求代理服务器?

[英]Using C# WebClient with a Proxy - no request made to proxy server?

We have a background operation (Window service) that we want to use through a proxy server. 我们有一个后台操作(Window服务),我们想通过代理服务器使用它。

Basically, we're doing this: 基本上,我们这样做:

public WebClient GetWebClient(){
   var webClient = new WebClient();
   webClient.proxy = new WebProxy(Configuration.ProxyHost, Configuration.ProxyPort);

   // add a bunch of headers to the WebClient (sessionids, etc.)

   return webClient;
}

The proxy is one that we have configured ourselves using FreeProxy . 代理是我们使用FreeProxy自行配置的代理。

I've enabled logging and on the machine I'm testing with, and can confirm that requests are being made to the proxy when using it in Firefox. 我已经在我正在测试的机器上启用了日志记录,并且可以确认在Firefox中使用它时对代理进行了请求。

No authentication is required for the proxy server, except that the IP has to be within our office (which from the Firefox evidence, I assume is not the problem). 代理服务器不需要身份验证,除了IP必须在我们的办公室内(从Firefox证据,我认为不是问题)。

However, within our background process, I don't seem to be using the proxy when I use the webclient: 但是,在我们的后台进程中,当我使用webclient时,我似乎没有使用代理:

using(var wc = GetWebClient())
using(var s = wc.OpenRead("someurl"))
using(var sr = new StreamReader(s)){
    return sr.ReadToEnd();
}

I receive no errors from the proxy however, it seems like we're just going along without it even though the proxy has explicitly been set. 我没有从代理收到任何错误,但是,即使代理已经明确设置,似乎我们只是没有它。

The information seems to return fine, just not through our proxy. 信息似乎返回正常,而不是通过我们的代理。

Is there something I'm missing when using a proxy with a WebClient ? 使用带有WebClient的代理时,我有什么遗漏的东西吗?

edit: more details. 编辑:更多细节。 If we disable the proxy service on the server, then we get an exception that we can't connect. 如果我们在服务器上禁用代理服务,那么我们会得到一个我们无法连接的异常。 So it seems like the webclient is attempting to reach out to the proxy, but that traffic is not actually flowing through the proxy. 因此,似乎webclient正在尝试联系代理,但该流量实际上并未流经代理。

Inner Exception: SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

It turns out that FreeProxy wasn't accepting HTTPS traffic. 事实证明,FreeProxy不接受HTTPS流量。

I guess the proxy must return the types of traffic it can route, and if it cannot, the webclient does nothing. 我猜代理必须返回它可以路由的流量类型,如果不能,则webclient什么也不做。

Switched to using the Burp suite as our proxy since it can accept HTTPS. 切换到使用Burp套件作为我们的代理,因为它可以接受HTTPS。

http://portswigger.net/burp/ http://portswigger.net/burp/

您必须默认配置窗口以使用代理,或者在代码中手动设置代理,请参阅: http//msdn.microsoft.com/en-us/library/system.net.webclient.proxy(v = VS.100)的.aspx

You are using the WebClient class correctly as far as I can tell. 就我所知,您正在使用WebClient类。 I am able to see the following request... 我能看到以下要求......

using(var client = new WebClient())
{
    client.Proxy = new WebProxy("localhost", 8888);
    Console.WriteLine(client.DownloadString("http://www.google.com"));
}

in Fiddler running on my local box. 在Fiddler在我当地的盒子里跑。 Now if I shut Fiddler down, I get the WebException: 现在,如果我关闭Fiddler,我会得到WebException:

Unable to connect to the remote server

With an inner SocketException of: 内部SocketException:

No connection could be made because the target machine actively refused it 127.0.0.1:8888

So, with that said, my guess is that your proxy is working as intened but it is just not logging the outgoing HTTP request. 所以,说到这一点,我的猜测是你的代理工作是有条件的,但它只是没有记录传出的HTTP请求。

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

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