4

我们有一个要通过代理服务器使用的后台操作(Window 服务)。

基本上,我们这样做:

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

代理是我们使用FreeProxy自己配置的代理。

我已经在我正在测试的机器上启用了日志记录,并且可以确认在 Firefox 中使用代理时正在向代理发出请求。

代理服务器不需要身份验证,除了 IP 必须在我们的办公室内(从 Firefox 的证据来看,我认为这不是问题)。

但是,在我们的后台进程中,当我使用 webclient 时,我似乎没有使用代理:

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

我没有收到来自代理的错误,但似乎我们只是在没有它的情况下继续前进,即使代理已明确设置。

信息似乎可以正常返回,只是不是通过我们的代理。

使用带有 a 的代理时我缺少什么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
4

3 回答 3

1

事实证明,FreeProxy 不接受 HTTPS 流量。

我猜代理必须返回它可以路由的流量类型,如果不能,webclient 什么也不做。

切换到使用 Burp 套件作为我们的代理,因为它可以接受 HTTPS。

http://portswigger.net/burp/

于 2011-10-27T17:42:43.623 回答
0

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

于 2011-10-20T03:56:11.810 回答
0

据我所知,您正在正确使用 WebClient 类。我可以看到以下请求...

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

在我本地盒子上运行的提琴手。现在如果我关闭 Fiddler,我会得到 WebException:

Unable to connect to the remote server

内部 SocketException 为:

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

因此,话虽如此,我的猜测是您的代理正在按预期工作,但它只是没有记录传出的 HTTP 请求。

于 2011-10-21T00:46:12.767 回答