将C#WebClient与代理一起使用-是否未向代理服务器发出请求?
我们希望通过代理服务器使用后台操作(窗口服务) 基本上,我们正在这样做:将C#WebClient与代理一起使用-是否未向代理服务器发出请求?,c#,windows-services,webclient,proxies,C#,Windows Services,Webclient,Proxies,我们希望通过代理服务器使用后台操作(窗口服务) 基本上,我们正在这样做: public WebClient GetWebClient(){ var webClient = new WebClient(); webClient.proxy = new WebProxy(Configuration.ProxyHost, Configuration.ProxyPort); // add a bunch of headers to the WebClient (sessionids,
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;
}
这个代理是我们自己配置的
我已经在我正在测试的机器上启用了日志记录,并且可以确认在Firefox中使用代理时是否向其发出了请求
代理服务器不需要身份验证,除了IP必须在我们的办公室内(根据Firefox的证据,我认为这不是问题)
但是,在我们的后台流程中,我在使用webclient时似乎没有使用代理:
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
您必须在默认情况下配置windows以使用代理,或者在代码中手动设置代理,请参阅:据我所知,您正在正确使用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
内部插座除外:
No connection could be made because the target machine actively refused it 127.0.0.1:8888
这么说来,我猜您的代理正在按预期工作,但它只是没有记录传出的HTTP请求。事实证明FreeProxy没有接受HTTPS流量
我猜代理必须返回它可以路由的流量类型,如果不能,webclient什么也不做
切换到使用Burp套件作为代理,因为它可以接受HTTPS
我正在手动设置代理,如第一个方法片段所示。我是不是遗漏了什么?啊,对不起,我完全忽略了!让我检查一下在中断后是否有任何问题。我在测试时也做了同样的假设。但是,我可以指向一个我知道将返回403(禁止)作为我的机器地址的服务器,并且可以确认403已返回给我,即使在通过代理时也是如此。