WebClient - The remote server returned an error: (403) Forbidden

青春壹個敷衍的年華 提交于 2019-12-21 07:27:37

问题


Opening a public page from browser works fine.

Downloading same page using WebClient throws - (403) Forbidden.

What is going on here ?

Here is quick copy/paste example (used on console app) to specific page on web:

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

回答1:


I've just tried it with Fiddler running to see the response and it returns the following notice with the status code.

Scripts should use an informative User-Agent string with contact information, or they may be IP-blocked without notice.

This works.

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



回答2:


Check if the server, you are trying to access is set up to use improved TLS protocol. Make sure to add this to Global.asax.cs

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;


来源:https://stackoverflow.com/questions/2794260/webclient-the-remote-server-returned-an-error-403-forbidden

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!