2015-03-31 82 views
-2

我有這個功能來下載網站的html代碼,但是當我輸入這個特定的網站時,它返回一個點(。)而不是html代碼,任何人都可以告訴我什麼是錯或爲什麼是它不會重新調整代碼?Webclient下載字符串返回意外的結果

網站: 「http://bato.to/comic/_/nisekoi-r951

代碼:

 public string DownloadString(string add) 
     { 
      string html = "";    
      using (WebClient client = new WebClient()) 
      { 
       client.Proxy = null; 
       client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");     
       while (html=="") 
       { 
        try 
        { 
         html = client.DownloadString(add); 
         //MessageBox.Show(html);  
        } 
        catch 
        { 
         html = ""; 
        } 
       } 
       client.Dispose(); 
      } 
      return html; 
     } 

謝謝你的幫助。

+0

其實是否返回*空*或者它拋出一個異常,並因此設置爲空字符串? – Blorgbeard 2015-03-31 01:02:59

+0

首先將'httl://'轉換爲'http://' – SimpleVar 2015-03-31 01:03:30

+0

它返回null,因爲它在其他網站上有效。 – NoobCS 2015-03-31 01:03:37

回答

1

你需要使用 「HTTPS」 在該網站上:

public static string DownloadString(string add) 
     { 
      using (var client = new WebClient()) 
      { 
       client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)"); 
       return client.DownloadString(add); 
      } 
     } 

調用代碼:

Console.WriteLine(DownloadString("https://bato.to/comic/_/nisekoi-r951")); 

樣本響應:

   <div class='ipbfs_login_col'> 
        <input type='checkbox' id='inline_invisible' name='anonymous 
' value='1' class='input_check left' /> 
        <div style='padding-left: 20px;'> 
         <label for='inline_invisible'> 
          <strong>Sign in anonymously</strong> 
          <span class='desc lighter' style='display: block; pa 
dding-top: 5px;'>Don't add me to the active users list</span> 
         </label> 
        </div> 
       </div> 
+0

感謝這項工作。 – NoobCS 2015-03-31 01:25:29