使用带有代理的 C# WebClient - 没有向代理服务器发出请求?

发布于 2024-12-11 02:27:03 字数 1219 浏览 3 评论 0原文

我们有一个后台操作(窗口服务),我们想通过代理服务器使用它。

基本上,我们正在这样做:

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 的证据来看,我认为这不是问题)。

然而,在我们的后台进程中,当我使用网络客户端时,我似乎没有使用代理:

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

我没有收到来自代理的错误,但是,即使代理已明确地被使用,我们似乎也只是在没有它的情况下继续进行。放。

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

使用带有 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

We have a background operation (Window service) that we want to use through a proxy server.

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.

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.

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

However, within our background process, I don't seem to be using the proxy when I use the 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?

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

挖个坑埋了你 2024-12-18 02:27:03

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

我猜代理必须返回它可以路由的流量类型,如果不能,则 Web 客户端不执行任何操作。

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

http://portswigger.net/burp/

It turns out that FreeProxy wasn't accepting HTTPS traffic.

I guess the proxy must return the types of traffic it can route, and if it cannot, the webclient does nothing.

Switched to using the Burp suite as our proxy since it can accept HTTPS.

http://portswigger.net/burp/

难理解 2024-12-18 02:27:03

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

You either have to configure windows for using the proxy by default, or set the proxy manually in your code, see: http://msdn.microsoft.com/en-us/library/system.net.webclient.proxy(v=VS.100).aspx

望笑 2024-12-18 02:27:03

据我所知,您正在正确使用 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

With an inside SocketException of:

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

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

You are using the WebClient class correctly as far as I can tell. 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. Now if I shut Fiddler down, I get the WebException:

Unable to connect to the remote server

With an inner SocketException of:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文