2012-07-20 121 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