繁体   English   中英

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

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

我们有一个后台操作(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();
}

我没有从代理收到任何错误,但是,即使代理已经明确设置,似乎我们只是没有它。

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

使用带有WebClient的代理时,我有什么遗漏的东西吗?

编辑:更多细节。 如果我们在服务器上禁用代理服务,那么我们会得到一个我们无法连接的异常。 因此,似乎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

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

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

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

http://portswigger.net/burp/

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

就我所知,您正在使用WebClient类。 我能看到以下要求......

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

在Fiddler在我当地的盒子里跑。 现在,如果我关闭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请求。

暂无
暂无

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

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