Java 使用套接字请求时出现随机 unicode 字符
Java Random unicode character is appearing when using socket requesting
public final static String api = "api.hypixel.net";
public final static String toGet = "/skyblock/auctions";
public static void main(String[] args) {
try {
SSLSocket socket = (SSLSocket) SSLSocketFactory.getDefault().createSocket(api, 443);
socket.setSoTimeout(500);
PrintWriter pw = new PrintWriter(socket.getOutputStream());
pw.println("GET " + toGet + "?page=" + 1 + " HTTP/1.1");
pw.println("Host: " + api);
pw.println("");
pw.flush();
System.out.println("Wrote Socket: " + 1);
BufferedReader bufRead = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8));
StringBuilder sb = new StringBuilder();
String e;
try {
while ((e = bufRead.readLine()) != null) {
sb.append(e);
}
} catch (SocketTimeoutException easports) {
//
}
System.out.println("finished write");
bufRead.close();
pw.close();
String[] splitted = sb.toString().split("\{");
String copyofArr = String.join("{", Arrays.copyOfRange(splitted, 1, splitted.length));
String formatted = "{" + copyofArr.substring(0, copyofArr.length() - 1);
FileOutputStream fos = new FileOutputStream("gg1ay.txt");
fos.write(formatted.getBytes());
fos.flush();
fos.close();
JsonObject array = JsonParser.parseString(formatted).getAsJsonObject();
array.entrySet().forEach(entry -> System.out.println(entry.getKey() + " : " + entry.getValue()));
System.out.println("Got result: " + 1);
} catch (IOException e) {
System.err.println("an Exception");
e.printStackTrace();
}
}
当我使用此代码请求时,输出会出现一个奇怪的字母表(因此我无法解析 json)。它看起来像 unicode 字符,所以我附加了 \u,现在它显示了奇怪的中文字母表(如耀、翸、磛)。我该如何修复此代码?
我不知道它为什么起作用,但我通过将 http 版本降级到 1.0 解决了这个问题。
public final static String api = "api.hypixel.net";
public final static String toGet = "/skyblock/auctions";
public static void main(String[] args) {
try {
SSLSocket socket = (SSLSocket) SSLSocketFactory.getDefault().createSocket(api, 443);
socket.setSoTimeout(500);
PrintWriter pw = new PrintWriter(socket.getOutputStream());
pw.println("GET " + toGet + "?page=" + 1 + " HTTP/1.1");
pw.println("Host: " + api);
pw.println("");
pw.flush();
System.out.println("Wrote Socket: " + 1);
BufferedReader bufRead = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8));
StringBuilder sb = new StringBuilder();
String e;
try {
while ((e = bufRead.readLine()) != null) {
sb.append(e);
}
} catch (SocketTimeoutException easports) {
//
}
System.out.println("finished write");
bufRead.close();
pw.close();
String[] splitted = sb.toString().split("\{");
String copyofArr = String.join("{", Arrays.copyOfRange(splitted, 1, splitted.length));
String formatted = "{" + copyofArr.substring(0, copyofArr.length() - 1);
FileOutputStream fos = new FileOutputStream("gg1ay.txt");
fos.write(formatted.getBytes());
fos.flush();
fos.close();
JsonObject array = JsonParser.parseString(formatted).getAsJsonObject();
array.entrySet().forEach(entry -> System.out.println(entry.getKey() + " : " + entry.getValue()));
System.out.println("Got result: " + 1);
} catch (IOException e) {
System.err.println("an Exception");
e.printStackTrace();
}
}
当我使用此代码请求时,输出会出现一个奇怪的字母表(因此我无法解析 json)。它看起来像 unicode 字符,所以我附加了 \u,现在它显示了奇怪的中文字母表(如耀、翸、磛)。我该如何修复此代码?
我不知道它为什么起作用,但我通过将 http 版本降级到 1.0 解决了这个问题。