从浏览器打开公共(public)页面工作正常。
使用 WebClient 下载同一页面会抛出 - (403) Forbidden。
这是怎么回事?
这是快速复制/粘贴示例(用于控制台应用程序)到网络上的特定页面:
try
{
WebClient webClient = new WebClient();
string content = webClient.DownloadString("http://he.wikisource.org/wiki/%D7%A9%D7%95%D7%9C%D7%97%D7%9F_%D7%A2%D7%A8%D7%95%D7%9A_%D7%90%D7%95%D7%A8%D7%97_%D7%97%D7%99%D7%99%D7%9D_%D7%90_%D7%90");
}
catch (Exception ex)
{
throw;
}
最佳答案
我刚刚在运行 Fiddler 的情况下尝试查看响应,它返回以下带有状态代码的通知。
Scripts should use an informative User-Agent string with contact information, or they may be IP-blocked without notice.
这有效。
WebClient webClient = new WebClient();
webClient.Headers.Add("user-agent", "Only a test!");
string content = webClient.DownloadString("http://he.wikisource.org/wiki/%D7%A9%D7%95%D7%9C%D7%97%D7%9F_%D7%A2%D7%A8%D7%95%D7%9A_%D7%90%D7%95%D7%A8%D7%97_%D7%97%D7%99%D7%99%D7%9D_%D7%90_%D7%90");
关于c# - WebClient - 远程服务器返回错误 : (403) Forbidden,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2794260/