2011-10-20 47 views
4

我們有一個我們想通過代理服務器使用的後臺操作(Window服務)。將C#WebClient與代理服務器結合使用 - 對代理服務器沒有請求?

基本上,我們這樣做:

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證據來看,我認爲這不是問題)。

然而,我們的後臺進程中,我似乎不被使用代理,當我使用Web客戶端:

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

我沒有收到來自但是,代理錯誤,好像我們只是即使代理已經明確設置,也沒有它。

該信息似乎沒有問題,只是不通過我們的代理。

使用WebClient代理服務器時是否有某些我丟失?

編輯:更多細節。如果我們禁用服務器上的代理服務,那麼我們會得到一個我們無法連接的異常。所以看起來web客戶端正試圖接觸到代理服務器,但該流量實際上並未流經代理服務器。

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 

回答

1

事實證明,FreeProxy不接受HTTPS流量。

我想代理服務器必須返回它可以路由的流量類型,如果它不能,webclient什麼都不做。

由於可以接受HTTPS,因此使用Burp套件作爲我們的代理進行切換。

http://portswigger.net/burp/

0

您正在使用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 

隨着內SocketException:

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

因此,隨着中說,我的猜測是,你的代理工作作爲intened但它只是不記錄傳出的HTTP請求。

+0

我在測試時假定了同樣的事情。但是,我可以指向一臺我知道的服務器將返回一個403(禁止)我的機器地址,並且可以確認403即使在通過代理時也會返回給我。 – ddango