Skip to content

Commit 3464db8

Browse files
committed
重写 HTTP 部分以缩减插件文件大小
1 parent 813ecbe commit 3464db8

File tree

3 files changed

+42
-149
lines changed

3 files changed

+42
-149
lines changed

MiraiMC-Base/pom.xml

-12
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,6 @@
3131
<version>33.2.1-jre</version>
3232
<scope>provided</scope>
3333
</dependency>
34-
<dependency>
35-
<groupId>commons-codec</groupId>
36-
<artifactId>commons-codec</artifactId>
37-
<version>1.17.0</version>
38-
<scope>compile</scope>
39-
</dependency>
40-
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
41-
<dependency>
42-
<groupId>org.apache.httpcomponents</groupId>
43-
<artifactId>httpclient</artifactId>
44-
<version>4.5.14</version>
45-
</dependency>
4634
<dependency>
4735
<groupId>com.google.code.gson</groupId>
4836
<artifactId>gson</artifactId>

MiraiMC-Base/src/main/java/me/dreamvoid/miraimc/internal/Utils.java

+42-49
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
package me.dreamvoid.miraimc.internal;
22

3+
import com.google.gson.Gson;
34
import com.google.gson.JsonObject;
45
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;
126
import org.jetbrains.annotations.NotNull;
137

148
import java.io.*;
@@ -110,61 +104,60 @@ public static final class Http {
110104
*/
111105
public static String get(String url) throws IOException {
112106
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+
113115
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+
}
129122
}
130-
br.close();
131-
read.close();
132-
input.close();
133-
httpUrlConn.disconnect();
134123

124+
connection.disconnect();
135125
return sb.toString();
136126
}
137127

138128
/**
139129
* 发送HTTP POST请求
140130
* @param json Gson对象
141-
* @param URL 链接
131+
* @param url 链接
142132
* @return 远程服务器返回内容
143133
*/
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();
165157
}
166-
return strber.toString();
167158
}
159+
160+
return sb.toString();
168161
}
169162

170163
/**

MiraiMC-Base/src/main/resources/cmd.txt

-88
This file was deleted.

0 commit comments

Comments
 (0)