2012-08-14 91 views
0

我用這一小段代碼訪問遠程的URI:代理錯誤訪問https

Uri uri = "http://www.myurl.com"; 
WebRequest wreq = WebRequest.Create(uri); 
((HttpWebRequest)wreq).UserAgent = "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.75 Safari/537.1"; 
wreq.Proxy = WebRequest.DefaultWebProxy; 

這是所有「HTTP」 URI可以正常使用。但是,當切換到以下類型的Uri(https)時,我得到一個請求驗證的代理錯誤407(Exception的日誌告訴憑證不好)。你有什麼想法我可以如何處理這個?

Uri uri = "https://www.myurl.com"; 
WebRequest wreq = WebRequest.Create(uri); 
((HttpWebRequest)wreq).UserAgent = "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.75 Safari/537.1"; 
wreq.Proxy = WebRequest.DefaultWebProxy; 

最好的問候,

Al_th

+0

您在使用系統代理範圍的設置。從控制面板檢查IE代理設置或互聯網設置(假設您使用的是Windows)並查看您是否可以從IE訪問https://www.myurl.com網站。可能是你的https代理設置不好? – jpe 2012-08-14 08:29:06

+0

從控制面板,我有一個代理IP /端口定義,並選中「對所有協議使用相同的代理服務器」。 我可以訪問到https://www.myurl.com而沒有任何問題從IE瀏覽器,並且相同的鉻(這是沒有以任何方式由系統管理員配置,因爲我「上載」它後,上電腦,和不需要任何安裝,因此繞過了我工作地點的安裝安全策略。我假設所使用的憑證存儲在計算機上,但我認爲DefaultWebProxy使用與Chrome/IE相同的配置:x – 2012-08-14 09:27:40

回答

1

試試這個

private string GetPageSource(string url) 
{ 
    string htmlSource = string.Empty; 
    try 
    { 
     System.Net.WebProxy myProxy = new System.Net.WebProxy("Proxy IP", 8080); 
     using (System.Net.WebClient client = new System.Net.WebClient()) 
     { 
      client.Proxy = myProxy; 
      client.Proxy.Credentials = new System.Net.NetworkCredential("username", "password"); 
      htmlSource = client.DownloadString(url); 
     } 
    } 
    catch (WebException ex) 
    { 
     // log any exceptions 
    } 
    return htmlSource; 
} 
+0

哪個用戶名/我應該使用的密碼?我的會話登錄信息? 不應該包含在DefaultWebProxy中? – 2012-08-14 09:23:35

+0

如果是,那麼當瀏覽到https://yoururl.com時是否會彈出任何登錄框?那麼您應該將這些憑據添加爲登錄並通過,至少如果你知道他們 – JohnnBlade 2012-08-14 09:30:48

+0

嘗試取消註釋//以client.Proxy開始的行並再次嘗試你的https網址,看看你得到了什麼結果 – JohnnBlade 2012-08-14 09:31:48