|
1 | 1 | package org.CreadoresProgram.ServerWebGamePost.client; |
| 2 | + |
2 | 3 | import lombok.Getter; |
3 | 4 | import lombok.Setter; |
4 | 5 | import lombok.NonNull; |
5 | 6 | import com.alibaba.fastjson2.JSONObject; |
6 | 7 | import com.alibaba.fastjson2.JSON; |
7 | | -import java.net.HttpURLConnection; |
8 | | -import java.net.URL; |
9 | | -import java.io.BufferedReader; |
10 | | -import java.io.InputStreamReader; |
11 | | -import java.io.OutputStream; |
12 | | -import java.io.IOException; |
| 8 | +import org.jsoup.Jsoup; |
| 9 | +import org.jsoup.Connection; |
13 | 10 |
|
14 | | -public final class ServerWebGamePostClient{ |
| 11 | +public final class ServerWebGamePostClient { |
15 | 12 | private String domain; |
16 | 13 | private int port; |
17 | 14 | private boolean isHttps; |
| 15 | + |
18 | 16 | @Getter |
19 | 17 | @Setter |
20 | 18 | public ProcessDatapackClient processDatapacks; |
21 | 19 |
|
22 | 20 | @Getter |
23 | 21 | @Setter |
24 | | - public String userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0"; |
| 22 | + public String userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36 Edg/134.0.0.0"; |
25 | 23 |
|
26 | | - public ServerWebGamePostClient(@NonNull String domain, @NonNull int port, @NonNull boolean isHttps){ |
| 24 | + public ServerWebGamePostClient(@NonNull String domain, @NonNull int port, @NonNull boolean isHttps) { |
27 | 25 | this.domain = domain; |
28 | 26 | this.port = port; |
29 | 27 | this.isHttps = isHttps; |
30 | 28 | this.processDatapacks = new ProcessDatapackClient(this); |
31 | 29 | } |
32 | | - public void sendDataPacket(@NonNull JSONObject datapack){ |
33 | | - try{ |
34 | | - String prefix = ""; |
35 | | - if(this.isHttps){ |
36 | | - prefix = "https://"; |
37 | | - }else{ |
38 | | - prefix = "http://"; |
39 | | - } |
40 | | - String datapackstr = datapack.toJSONString(); |
41 | | - HttpURLConnection serverFtch = (HttpURLConnection) new URL(prefix+this.domain+":"+this.port+"/ServerWebGamePost").openConnection(); |
42 | | - serverFtch.setRequestMethod("POST"); |
43 | | - serverFtch.setRequestProperty("User-Agent", this.userAgent); |
44 | | - serverFtch.setRequestProperty("Content-Type", "application/json; utf-8"); |
45 | | - serverFtch.setDoOutput(true); |
46 | | - try(OutputStream osftch = serverFtch.getOutputStream()){ |
47 | | - byte[] inputftch = datapackstr.getBytes("utf-8"); |
48 | | - osftch.write(inputftch, 0, inputftch.length); |
49 | | - }catch(IOException e){ |
50 | | - System.err.println(e); |
51 | | - } |
52 | | - try(BufferedReader brftch = new BufferedReader(new InputStreamReader(serverFtch.getInputStream(), "utf-8"))){ |
53 | | - StringBuilder responseftch = new StringBuilder(); |
54 | | - String line; |
55 | | - while((line = brftch.readLine()) != null){ |
56 | | - responseftch.append(line.trim()); |
57 | | - } |
58 | | - this.processDatapacks.process(JSON.parseObject(responseftch.toString())); |
59 | | - }catch(IOException e){ |
60 | | - System.err.println(e); |
| 30 | + |
| 31 | + public void sendDataPacket(@NonNull JSONObject datapack) { |
| 32 | + try { |
| 33 | + String prefix = this.isHttps ? "https://" : "http://"; |
| 34 | + String datapackstr = datapack.toJSONString(); |
| 35 | + String url = prefix + this.domain + ":" + this.port + "/ServerWebGamePost"; |
| 36 | + |
| 37 | + Connection.Response response = Jsoup |
| 38 | + .connect(url) |
| 39 | + .userAgent(this.userAgent) |
| 40 | + .header("Content-Type", "application/json") |
| 41 | + .requestBody(datapackstr) |
| 42 | + .method(Connection.Method.POST) |
| 43 | + .ignoreContentType(true) |
| 44 | + .execute(); |
| 45 | + |
| 46 | + this.processDatapacks.process(JSON.parseObject(response.body())); |
| 47 | + } catch(Exception erd) { |
| 48 | + System.err.println(erd); |
61 | 49 | } |
62 | | - }catch(Exception erd){ |
63 | | - System.err.println(erd); |
64 | | - } |
65 | 50 | } |
66 | 51 | } |
0 commit comments