2012-07-20 204 views
1

嗨如何通過代理獲取html頁面的源代碼。當我使用下面的代碼時,出現錯誤,提示「需要代理身份驗證」。我必須通過代理。通過代理獲取html源代碼

Dim client As New WebClient() 

Dim htmlCode As String = client.DownloadString("http://www.stackoverflow.com") 

回答

3

然後使用不需要身份驗證

看到這裏獲取更多信息 http://msdn.microsoft.com/en-us/library/system.net.webclient.proxy.aspx

string source = GetPageSource("http://www.stackoverflow.com"); 

    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

一個代理,但我必須去通過代理 – 2012-07-20 08:07:49

+0

現在您只需要將其轉換爲VB.Net,但應該很容易 – JohnnBlade 2012-07-20 08:15:07

+0

你測試過嗎? – JohnnBlade 2012-07-20 08:23:43