Webclient Download String返回意外结果

时间:2015-03-31 01:01:14

标签: c# webclient

我有这个功能来下载网站的html代码但是当我输入这个特定的网站时,它返回一个点(。)而不是html代码,任何人都可以告诉我什么是错的或为什么它不重新调整代码?

网站: “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;
        }

感谢您的帮助。

1 个答案:

答案 0 :(得分: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>