簡體   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