Skip to content

Commit ee93b62

Browse files
committed
fix: http connection closed
1 parent 1791056 commit ee93b62

1 file changed

Lines changed: 13 additions & 6 deletions

File tree

src/main/java/com/github/hank9999/useblessingskin/shared/HttpMethods.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
import javax.imageio.ImageIO;
77
import java.awt.image.BufferedImage;
88
import java.io.*;
9+
import java.nio.charset.StandardCharsets;
910
import java.nio.file.Files;
1011
import java.nio.file.Path;
1112
import java.nio.file.Paths;
1213

1314
final public class HttpMethods {
1415
private static final OkHttpClient client = new OkHttpClient();
1516

16-
public static ResponseBody get(String url) throws Exception {
17+
public static byte[] get(String url) throws Exception {
1718
Request request = new Request.Builder()
1819
.url(url)
1920
.header("User-Agent", "UseBlessingSkinPlugin/1.0")
@@ -28,13 +29,19 @@ public static ResponseBody get(String url) throws Exception {
2829
throw new IOException("HTTP error code: " + response.code() + " | " + response.message());
2930
}
3031

31-
return response.body();
32+
ResponseBody body = response.body();
33+
34+
if (body == null) {
35+
return null;
36+
}
37+
38+
return body.bytes();
3239
}
3340
}
3441

3542
public static String getString(String url) throws Exception {
36-
ResponseBody body = get(url);
37-
return body != null ? body.string() : null;
43+
byte[] body = get(url);
44+
return body != null ? new String(body, StandardCharsets.UTF_8) : null;
3845
}
3946

4047
public static boolean getPicture(String urlHttp, String path, String picName) throws Exception {
@@ -47,12 +54,12 @@ public static boolean getPicture(String urlHttp, String path, String picName) th
4754
File picFile = new File(path, picName);
4855

4956
// 读取
50-
ResponseBody body = get(urlHttp);
57+
byte[] body = get(urlHttp);
5158
if (body == null) {
5259
return false;
5360
}
5461

55-
try (InputStream inputStream = body.byteStream()) {
62+
try (InputStream inputStream = new ByteArrayInputStream(body)) {
5663
BufferedImage img = ImageIO.read(inputStream);
5764
Boolean imgValid = Utils.checkImgValid(img);
5865

0 commit comments

Comments
 (0)