Java 8 https 不一致请求
您好,我尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 HttpURLConnection 类的导入语句从 修改为
当
您尝试使用上述 JDK 13+ 的代码并重新导入包时,
会导致应用程序代码中的 sun 包和相关类位于 JVM 内部。
Modify your import statement for HttpURLConnection class from
to the
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.