C# Consolse Apps 出现错误远程服务器返回错误:(403) 禁止使用 WebClient 和 HttpWebRequest
我是这个编程世界的新手。所以我有一个任务是通过 C# 控制台应用程序从 url 下载 zip 文件,我已经尝试了很多方法,但仍然遇到相同的错误。我已经基于 Fiddler 结果在我的代码中添加了一些标头。
这是我的代码:
WebClient webclient = new WebClient();
webclient.Headers.Add("sec-ch-ua-mobile", "?0");
webclient.Headers.Add("sec-ch-ua-platform", "windows");
webclient.Headers.Add("upgrade-insecure-requests", "1");
webclient.Headers.Add("user-agent", "mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/98.0.4758.102 safari/537.36");
webclient.Headers.Add("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,/;q=0.8,application/signed-exchange;v=b3;q=0.9");
webclient.Headers.Add("sec-fetch-site", "cross-site");
webclient.Headers.Add("sec-fetch-mode", "navigate");
webclient.Headers.Add("sec-fetch-user", "?1");
webclient.Headers.Add("sec-fetch-dest", "document");
webclient.Headers.Add("accept-encoding", "gzip, deflate, br");
webclient.Headers.Add("accept-language", "id-id,id;q=0.9,en-us;q=0.8,en;q=0.7,ms;q=0.6,th;q=0.5");
webclient.DownloadFile(url, @"d:\data.zip");
有遗漏吗?或者我应该采取其他方式?
谢谢 问候, 阿尔文
i'm new in this programming world. So i got a task to download zip file from url via C# Console Application, i've tried many things but still got same error. I've already add some Headers in my code also based on Fiddler Result.
This is my code:
WebClient webclient = new WebClient();
webclient.Headers.Add("sec-ch-ua-mobile", "?0");
webclient.Headers.Add("sec-ch-ua-platform", "windows");
webclient.Headers.Add("upgrade-insecure-requests", "1");
webclient.Headers.Add("user-agent", "mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/98.0.4758.102 safari/537.36");
webclient.Headers.Add("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,/;q=0.8,application/signed-exchange;v=b3;q=0.9");
webclient.Headers.Add("sec-fetch-site", "cross-site");
webclient.Headers.Add("sec-fetch-mode", "navigate");
webclient.Headers.Add("sec-fetch-user", "?1");
webclient.Headers.Add("sec-fetch-dest", "document");
webclient.Headers.Add("accept-encoding", "gzip, deflate, br");
webclient.Headers.Add("accept-language", "id-id,id;q=0.9,en-us;q=0.8,en;q=0.7,ms;q=0.6,th;q=0.5");
webclient.DownloadFile(url, @"d:\data.zip");
Is there any missed? or should i take other way?
Thanks
Regards,
Alvin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
403 是指 HTTP 被禁止。它通常使用 API_KEY 等身份验证或任何身份验证,但被拒绝或失败。
确保您的 API_KEY 正确。
https://developer.mozilla.org/en-US/ docs/Web/HTTP/Status/403
https://en.wikipedia.org/wiki/HTTP_403
403 is mean HTTP forbidden. It's usually using authentication like an API_KEY or any authentication but refused or fail.
Make sure your API_KEY is right.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/403
https://en.wikipedia.org/wiki/HTTP_403
您能否添加您要下载该 zip 的 URL?
Can you please add the URL from where you want to download that zip?