无法获取Web HTML代码:System.Net.WebException:'远程服务器返回错误:(403)禁止。

时间:2018-10-06 17:34:44

标签: c# vb.net webclient downloadstring

我的问题是我无法获取特定的网站html代码,并且出现此错误: 'System.Net.WebException:'远程服务器返回错误:(403)禁止。'

我的代码很简单:

using (WebClient client = new WebClient())
        {
            string htmlCode = client.DownloadString("http://isbnsearch.org/isbn/");
            MessageBox.Show(htmlCode);
        }

当我尝试使用Google之类的其他网站时,一切运行正常,但是无法访问此网站。

是否有解决此问题的解决方案? 谢谢

2 个答案:

答案 0 :(得分:0)

由于您无权访问isbnsearch.org,因此您可以捕获错误并避免应用崩溃,但无法解决。

using (WebClient client = new WebClient())
        {
            try
            {
                 string htmlCode = client.DownloadString("http://isbnsearch.org/isbn/");
                 MessageBox.Show(htmlCode);
            }
            catch (Exception e)
            { 
                 MessageBox.Show(e.Message);
            }
        }

答案 1 :(得分:-1)

找到解决此错误的解决方案:

        string url = "https://www.isbnsearch.org/";

        using (HttpClient client = new HttpClient())
        {
            using (HttpResponseMessage response = client.GetAsync(url).Result)
            {
                using (HttpContent content = response.Content)
                {
                    string result = content.ReadAsStringAsync().Result;
                    MessageBox.Show(result);
                }
            }
        }