Java 8 https 不一致请求

发布于 2025-01-11 05:09:56 字数 1468 浏览 0 评论 0原文

您好,我尝试使用 HttpsConnection 从不和谐服务器获取响应,但服务器响应错误 403,我不知道为什么,我在 JDK 13+ 以上尝试了此代码并且它有效,但我需要 JDK 8,但在这个版本上由于某种原因它不起作用。

错误:

线程“main”中出现异常 java.io.IOException:服务器返回 HTTP 响应代码:403 对于 URL: https://discord.com/api/v9/users/@me/channels< /a> 在 sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1897) 在 sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1495) 在 sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:268) 在 com.joojn.Main.main(Main.java:31)

String BASE_URL = "https://discord.com/api/v9";
String token = "superSecretToken";

String getChannelsURL = BASE_URL + "/users/@me/channels";
String getGuildsURL = BASE_URL + "/users/@me/guilds";

URL url = new URL(getChannelsURL);

HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("authorization", token);
        

InputStream content = (InputStream) connection.getInputStream(); // this is line 31
String text = new BufferedReader(new InputStreamReader(content, StandardCharsets.UTF_8)).lines().collect(Collectors.joining("\n"));

System.out.println(text);

Hello, I tried to get a response from a discord server using HttpsConnection, but the server responded with error 403 and I don't know why, I tried this code in JDK 13+ above and it worked, but I need JDK 8, but on this version it for some reason does not work.

The error:

Exception in thread "main" java.io.IOException: Server returned HTTP
response code: 403 for URL:
https://discord.com/api/v9/users/@me/channels at
sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1897)
at
sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1495)
at
sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:268)
at com.joojn.Main.main(Main.java:31)

String BASE_URL = "https://discord.com/api/v9";
String token = "superSecretToken";

String getChannelsURL = BASE_URL + "/users/@me/channels";
String getGuildsURL = BASE_URL + "/users/@me/guilds";

URL url = new URL(getChannelsURL);

HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("authorization", token);
        

InputStream content = (InputStream) connection.getInputStream(); // this is line 31
String text = new BufferedReader(new InputStreamReader(content, StandardCharsets.UTF_8)).lines().collect(Collectors.joining("\n"));

System.out.println(text);

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

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

发布评论

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

评论(1

君勿笑 2025-01-18 05:09:56

将 HttpURLConnection 类的导入语句从 修改为

import sun.net.www.protocol.http.HttpURLConnection;

import java.net.HttpURLConnection;

您尝试使用上述 JDK 13+ 的代码并重新导入包时,

会导致应用程序代码中的 sun 包和相关类位于 JVM 内部。

Modify your import statement for HttpURLConnection class from

import sun.net.www.protocol.http.HttpURLConnection;

to the

import java.net.HttpURLConnection;

When you try the code with JDK 13+ above and it re-import the package

cause the sun package and related class in your application code are internal to JVM.

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