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



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

我的代码很简单:

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

当我尝试使用谷歌等其他网站时,一切正常,但有了这个网站,我无法访问它。

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

因为您无法访问 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);
}
}

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

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);
}
}
}

最新更新