WebClient 403禁止使用
我可以在IE中手动下载。
http://scholar.google.com/scholar.ris?q=info:j8ymU9rzMsEJ:scholar.google.com/&output=citation&hl=zh-CN&as_sdt=2000&oe=GB&ct=citation&cd=0
但是,使用以下代码
WebClient客户端=新WebClient(); client.DownloadFile(address,filename);
显示例外:403禁止
怎么了? 我怎样才能做到这一点?
其他
http://scholar.google.com/scholar.ris?q=info:sskrpr5jlLwJ:scholar.google.com/&output=citation&hl=zh-CN&as_sdt=2000&oe=GB&ct=citation&cd=1
我在IE中得到403,我猜你需要登录才能检索资源。 您的浏览器可能已缓存凭据,但您的应用并非旨在让您登录。或者您是否在浏览器中登录Google – 尝试退出并查看您是否仍然可以访问….
只需在下载之前添加一行简单的行:
string url = ... string fileName = ... WebClient wb = new WebClient(); wb.Headers.Add("User-Agent: Other"); //that is the simple line! wb.DownloadFile(url, fileName);
而已。
在调用DownloadFile
方法之前,需要设置适当的http标头。
WebClient webClient = new WebClient(); webClient.Headers.Add("???", "???"); webClient.Headers.Add("???", "???"); webClient.Headers.Add("???", "???"); webClient.DownloadFile(address, filename);
放置正确的值而不是这些问号可能会很棘手。 您需要下载Fiddler或其他程序或webbrowser扩展程序,以显示您的webbrowser向Google发送的HTTP标头,并基本上在您的程序中复制相同的请求。
这就是我发生的事情:
我试图下载一个(公共).xls文件(通过DownloadFile方法),该文件可以从所有浏览器中轻松下载。
在尝试和挣扎所有答案(但没有运气)之后,我终于打开了堆栈并注意到一些奇怪的东西(参见截图)。
虽然,该文件是通过浏览器中的http下载的,但它通过DownloadFile方法给出403错误。
最后,我刚刚将URL从http://某些内容更改为https://某些内容并且运行正常。
希望这可以帮助!
403也可能由TLS问题引起。 要进行validation,您应该检查WebException.Response对象的文本。
catch (WebException ex) { if (ex.Response != null) { var response = ex.Response; var dataStream = response.GetResponseStream(); var reader = new StreamReader(dataStream); var details = reader.ReadToEnd(); } }
如果是TLS,请尝试将其添加到您的代码中以强制使用TLS1.2。
对于.net4:
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
对于.net4.5或更高版本:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
我遇到了同样的问题,试图在Amazon 3Surl上下载文件。 我在这里写了博客: http : //blog.cdeutsch.com/2010/11/net-webclient-403-forbidden-error.html
我在这里找到了最终解决方案: 使用url编码的斜杠获取URL
解决这个问题的关键是通过代码执行一次请求,第二次在浏览器中使用Fiddler记录这两个请求并确保标头匹配。
我最终不得不添加标题:
- 接受
- 接受编码
- 接受语言
- 用户代理
- 升级不安全,请求
我希望这有助于未来的人们。
我遇到了类似的问题,试图从几个特定网站下载文件导致某些文件返回403错误,但其他文件没有。
我尝试过User-Agent标头,接受标头,尝试httpsurl和各种其他设置,但仍然没有成功。
这两个URL都将在浏览器中加载,并且不需要在网站上进行任何身份validation即可访问它们(它们是公共访问权限),但是一个将下载而另一个将返回403。
对原因是什么以及如何解决的任何帮助。
static void Main(string[] args) { WebClient webClient = new WebClient(); webClient.Headers.Add("Accept: text/html, application/xhtml+xml, application/pdf, */*"); webClient.Headers.Add("User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)"); webClient.Headers.Add("Accept-Encoding: gzip, deflate, br"); webClient.Headers.Add("Accept-Language: en-US,en;q=0.9"); webClient.Headers.Add("Cache-Control: no-cache"); webClient.Headers.Add("Upgrade-Insecure-Requests: 1"); try { webClient.DownloadFile(new Uri("https://www.vigil.aero/wp-content/uploads/PSB-10-2013-06-14-.pdf"), "test1.pdf"); Console.WriteLine("Complete"); } catch (Exception ex) { Console.WriteLine("{0}", ex.Message); } try { webClient.DownloadFile(new Uri("https://www.vigil.aero/wp-content/uploads/PSB-9-2013-06-14.pdf"), "test2.pdf"); Console.WriteLine("Complete"); } catch (Exception ex) { Console.WriteLine("{0}", ex.Message); } Console.ReadLine(); }
尝试从SharePoint网站URL下载图像时遇到此问题。 在我的情况下,将user-agent
设置为其他或标题中的空白是不够的,我必须设置user-agent
,如下所示:
client.Headers.Add("user-agent", " Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0");
该解决方案来自这个答案 。