使用用户代理从 https url 下载 Java 仍然返回 403

发布于 2025-01-14 18:36:23 字数 76 浏览 3 评论 0原文

我尝试从 https URL 下载 gzip 文件,但它返回 403。我发现一些帖子说问题是由于缺少“用户代理”引起的,但它对我不起作用。

I tried to download a gzip file from a https URL, but it returns 403. I find some post said the problem caused by lacking "user-agent", but it didn't work for me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

违心° 2025-01-21 18:36:24

我找到了两种方法来解决这个问题:

  1. 设置cookie

     connection.setRequestProperty( "cookie", cookie );
    
  2. 如果您访问的网站需要令牌,请设置基本身份验证

     String 用户名 = "";
     字符串密码=“”;
     String auth = 用户名+":"+密码;
     byte[] rel = Base64.encodeBase64( auth.getBytes());
     字符串 res = new String(rel);
     connection.setRequestProperty("授权","基本" + res);
    

希望它适合您。

I found two ways to solve the problem:

  1. set cookie

     connection.setRequestProperty( "cookie", cookie );
    
  2. set basic auth if the website you access need token

     String userName = "";
     String password = "";
     String auth = userName+":"+password;
     byte[] rel = Base64.encodeBase64( auth.getBytes());
     String res = new String(rel);
     connection.setRequestProperty("Authorization","Basic " + res);
    

Hope it works for you.

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