2015-03-31 36 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