0

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

我的代码很简单:

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

当我尝试使用像谷歌这样的其他网站时,一切都很完美,但是使用这个网站我无法访问它。

有什么解决方案可以解决这个问题吗?谢谢

4

2 回答 2

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);
            }
        }
于 2018-10-06T17:42:45.537 回答
-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);
                }
            }
        }
于 2018-10-06T17:58:45.743 回答