C# Consolse Apps 出现错误远程服务器返回错误:(403) 禁止使用 WebClient 和 HttpWebRequest

发布于 2025-01-12 01:31:26 字数 1278 浏览 0 评论 0原文

我是这个编程世界的新手。所以我有一个任务是通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌逼全场 2025-01-19 01:31:26

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

风筝在阴天搁浅。 2025-01-19 01:31:26

您能否添加您要下载该 zip 的 URL?

Can you please add the URL from where you want to download that zip?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文