Java 8 个 https 不和谐请求
Java 8 https discord request
您好,我尝试使用 HttpsConnection 从 discord 服务器获取响应,但服务器响应错误 403,我不知道为什么,我在 JDK 中尝试了这段代码13+ 以上并且有效,但我需要 JDK 8,但由于某种原因在这个版本上它不起作用。
错误:
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);
从
修改 HttpURLConnection class 的导入语句
import sun.net.www.protocol.http.HttpURLConnection;
到
import java.net.HttpURLConnection;
当您尝试上面的 JDK 13+ 代码时,它 re-import 包
因为应用程序代码中的 sun 包和相关 class 是 JVM 内部的。
您好,我尝试使用 HttpsConnection 从 discord 服务器获取响应,但服务器响应错误 403,我不知道为什么,我在 JDK 中尝试了这段代码13+ 以上并且有效,但我需要 JDK 8,但由于某种原因在这个版本上它不起作用。
错误:
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);
从
修改 HttpURLConnection class 的导入语句import sun.net.www.protocol.http.HttpURLConnection;
到
import java.net.HttpURLConnection;
当您尝试上面的 JDK 13+ 代码时,它 re-import 包
因为应用程序代码中的 sun 包和相关 class 是 JVM 内部的。