|
1 | 1 | package me.dreamvoid.miraimc.internal;
|
2 | 2 |
|
| 3 | +import com.google.gson.Gson; |
3 | 4 | import com.google.gson.JsonObject;
|
4 | 5 | import me.dreamvoid.miraimc.internal.config.PluginConfig;
|
5 |
| -import org.apache.http.HttpResponse; |
6 |
| -import org.apache.http.HttpStatus; |
7 |
| -import org.apache.http.client.methods.HttpPost; |
8 |
| -import org.apache.http.entity.StringEntity; |
9 |
| -import org.apache.http.impl.client.CloseableHttpClient; |
10 |
| -import org.apache.http.impl.client.HttpClients; |
11 |
| -import org.apache.http.message.BasicHeader; |
12 | 6 | import org.jetbrains.annotations.NotNull;
|
13 | 7 |
|
14 | 8 | import java.io.*;
|
@@ -110,61 +104,60 @@ public static final class Http {
|
110 | 104 | */
|
111 | 105 | public static String get(String url) throws IOException {
|
112 | 106 | URL obj = new URL(url.replace(" ", "%20"));
|
| 107 | + HttpURLConnection connection = (HttpURLConnection) obj.openConnection(); |
| 108 | + |
| 109 | + connection.setDoInput(true); |
| 110 | + connection.setRequestMethod("GET"); |
| 111 | + connection.setRequestProperty("User-Agent", "Mozilla/5.0 DreamVoid MiraiMC"); |
| 112 | + connection.setConnectTimeout(5000); |
| 113 | + connection.setReadTimeout(10000); |
| 114 | + |
113 | 115 | StringBuilder sb = new StringBuilder();
|
114 |
| - HttpURLConnection httpUrlConn = (HttpURLConnection) obj.openConnection(); |
115 |
| - |
116 |
| - httpUrlConn.setDoInput(true); |
117 |
| - httpUrlConn.setRequestMethod("GET"); |
118 |
| - httpUrlConn.setRequestProperty("User-Agent", "Mozilla/5.0 DreamVoid MiraiMC"); |
119 |
| - httpUrlConn.setConnectTimeout(5000); |
120 |
| - httpUrlConn.setReadTimeout(10000); |
121 |
| - |
122 |
| - InputStream input = httpUrlConn.getInputStream(); |
123 |
| - InputStreamReader read = new InputStreamReader(input, StandardCharsets.UTF_8); |
124 |
| - BufferedReader br = new BufferedReader(read); |
125 |
| - String data = br.readLine(); |
126 |
| - while (data != null) { |
127 |
| - sb.append(data).append(System.lineSeparator()); |
128 |
| - data = br.readLine(); |
| 116 | + try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) { |
| 117 | + String data = br.readLine(); |
| 118 | + while (data != null) { |
| 119 | + sb.append(data).append(System.lineSeparator()); |
| 120 | + data = br.readLine(); |
| 121 | + } |
129 | 122 | }
|
130 |
| - br.close(); |
131 |
| - read.close(); |
132 |
| - input.close(); |
133 |
| - httpUrlConn.disconnect(); |
134 | 123 |
|
| 124 | + connection.disconnect(); |
135 | 125 | return sb.toString();
|
136 | 126 | }
|
137 | 127 |
|
138 | 128 | /**
|
139 | 129 | * 发送HTTP POST请求
|
140 | 130 | * @param json Gson对象
|
141 |
| - * @param URL 链接 |
| 131 | + * @param url 链接 |
142 | 132 | * @return 远程服务器返回内容
|
143 | 133 | */
|
144 |
| - public static String post(JsonObject json, String URL) throws IOException { |
145 |
| - try (CloseableHttpClient client = HttpClients.createDefault()) { |
146 |
| - HttpPost post = new HttpPost(URL); |
147 |
| - post.setHeader("Content-Type", "application/json"); |
148 |
| - post.addHeader("Authorization", "Basic YWRtaW46"); |
149 |
| - StringEntity s = new StringEntity(json.toString(), StandardCharsets.UTF_8); |
150 |
| - s.setContentType(new BasicHeader(org.apache.http.protocol.HTTP.CONTENT_TYPE, "application/json")); |
151 |
| - post.setEntity(s); |
152 |
| - // 发送请求 |
153 |
| - HttpResponse httpResponse = client.execute(post); |
154 |
| - // 获取响应输入流 |
155 |
| - InputStream inStream = httpResponse.getEntity().getContent(); |
156 |
| - BufferedReader reader = new BufferedReader(new InputStreamReader( |
157 |
| - inStream, StandardCharsets.UTF_8)); |
158 |
| - StringBuilder strber = new StringBuilder(); |
159 |
| - String line; |
160 |
| - while ((line = reader.readLine()) != null) |
161 |
| - strber.append(line).append("\n"); |
162 |
| - inStream.close(); |
163 |
| - if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { |
164 |
| - logger.warning("Http request returned bad status code: " + httpResponse.getStatusLine().getStatusCode()+", reason: "+ httpResponse.getStatusLine().getReasonPhrase()); |
| 134 | + public static String post(JsonObject json, String url) throws IOException { |
| 135 | + URL obj = new URL(url.replace(" ", "%20")); |
| 136 | + HttpURLConnection connection = (HttpURLConnection) obj.openConnection(); |
| 137 | + |
| 138 | + connection.setDoInput(true); |
| 139 | + connection.setRequestMethod("POST"); |
| 140 | + connection.setRequestProperty("User-Agent", "Mozilla/5.0 DreamVoid MiraiMC"); |
| 141 | + connection.setRequestProperty("Content-Type", "application/json"); |
| 142 | + connection.setRequestProperty("Authorization", "Basic YWRtaW46"); |
| 143 | + connection.setConnectTimeout(5000); |
| 144 | + connection.setReadTimeout(10000); |
| 145 | + |
| 146 | + try(DataOutputStream os = new DataOutputStream(connection.getOutputStream())){ |
| 147 | + os.write(new Gson().toJson(json).getBytes(StandardCharsets.UTF_8)); |
| 148 | + } |
| 149 | + |
| 150 | + StringBuilder sb = new StringBuilder(); |
| 151 | + |
| 152 | + try(BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()))){ |
| 153 | + String data = br.readLine(); |
| 154 | + while (data != null) { |
| 155 | + sb.append(data).append(System.lineSeparator()); |
| 156 | + data = br.readLine(); |
165 | 157 | }
|
166 |
| - return strber.toString(); |
167 | 158 | }
|
| 159 | + |
| 160 | + return sb.toString(); |
168 | 161 | }
|
169 | 162 |
|
170 | 163 | /**
|
|
0 commit comments