Home > Software design >  Java Random unicode character is appearing when using socket requesting
Java Random unicode character is appearing when using socket requesting

Time:02-19

    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();
        }
    }

When I request with this code, an weird alphabet comes with output (so i failed to parse json). it looks like unicode char, So I attached \u, now it shows weird chinese alphabet (like 耀, 翸, 磛). How can I fix this code?

CodePudding user response:

I don't know why its working, but i fixed this problem with downgrading http version to 1.0.

  • Related