我们有一个要通过代理服务器使用的后台操作(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