2011-05-31 45 views
8

我有一個windows窗體應用程序,其中包含WebBrowser控件。這個想法是WebBrowser在沒有用戶交互的情況下瀏覽網站。 WebBrowser通過代理訪問互聯網。帶自動代理登錄的WebBrowser控件

我可以看到代理服務器上發出的請求,但由於代理身份驗證失敗而被拒絕。

我已添加Proxy-Authorization: Basic標題。這適用於一個普通的HTTP頁面,但它似乎沒有工作的https:

var credentialStringValue = "proxyUser:proxyPassword"; 
byte[] credentialByteArray = ASCIIEncoding.ASCII.GetBytes(credentialStringValue); 
var credentialBase64String = Convert.ToBase64String(credentialByteArray); 

string Headers = string.Format("Proxy-Authorization: Basic {0}{1}", credentialBase64String, Environment.NewLine); 

ws.Navigate(url,TargetFrameName,PostData,Headers); 

哪裏ws等於new WebBrowser()。憑據是正確的,因爲它在我手動執行時有效。

有關如何以編程方式驗證代理憑證的任何想法?

+0

@Abat代碼 - 當然 - 沒有人。下一次你做一個雙重編輯,只需在編輯中添加理由。 – 2011-05-31 08:48:01

+0

@凱爾:當然!乾杯! :) – abatishchev 2011-05-31 09:09:22

回答

3
// do what you want with proxy class 
WebProxy webProxy = new WebProxy(host, port) 
{ 
    Credentials = ... 
} 

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://example.com"); 
webRequest.Proxy = webProxy; 

HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse(); 
Stream receiveStream = response.GetResponseStream(); 

WebBrowser webBrowser = new WebBrowser(); 
webBrowser.DocumentStream = receiveStream; 
+2

這不是OP所要求的。他想通過代理運行整個WebBrowser。你是否建議他按照你的建議重寫所有內容?他也可以做​​出控制。 – 2011-05-31 08:28:18

+0

@Kyle:我的提議是關於不使用'Navigate()',但通過WebRequest使用WebProxy獲取HTML,並使用WebBrowser控件來呈現它 – abatishchev 2011-05-31 08:42:27

+0

我明白了。當有人點擊網站上的鏈接時會發生什麼? – 2011-05-31 08:48:28

0

這些都沒有工作。由於Windows安全功能,它將始終彈出用戶名和密碼對話框。您首先必須將憑據存儲在Windows憑據中。你需要做的第一件事是通過NuGet包管理器下載'CredentialManagment'包。您首先必須使用用戶名和密碼將代理信息存儲在註冊表中。下面是註冊表

[DllImport("wininet.dll", SetLastError = true)] 
     public static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength); 
     public const int INTERNET_OPTION_SETTINGS_CHANGED = 39; 
     public const int INTERNET_OPTION_REFRESH = 37; 

static void setProxyRegistry(string proxyhost, bool proxyEnabled, string username, string password) 
     { 
      const string userRoot = "HKEY_CURRENT_USER"; 
      const string subkey = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"; 
      const string keyName = userRoot + "\\" + subkey; 

      Registry.SetValue(keyName, "ProxyServer", proxyhost, RegistryValueKind.String); 
      Registry.SetValue(keyName, "ProxyEnable", proxyEnabled ? "1" : "0", RegistryValueKind.DWord); 

      Registry.SetValue(keyName, "ProxyPass", password, RegistryValueKind.String); 
      Registry.SetValue(keyName, "ProxyUser", username, RegistryValueKind.String); 

      //<-loopback>;<local> 
      Registry.SetValue(keyName, "ProxyOverride", "*.local", RegistryValueKind.String); 


      // These lines implement the Interface in the beginning of program 
      // They cause the OS to refresh the settings, causing IP to realy update 
      InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0); 
      InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0); 
     } 

,那麼你需要設置憑據

Credential credentials= new Credential 
      { 
       Username = "Usernmae", 
       Password = "Password", 
       Target = "Target (usualy proxy domain)", 
       Type = CredentialType.Generic, 
       PersistanceType = PersistanceType.Enterprise 
      }; 
      credentials.Save(); 

我用這與.NET 4.5.2