From 368fdd14faf4117a462c4877af7bb2785e99ae44 Mon Sep 17 00:00:00 2001 From: hank9999 Date: Tue, 23 Jul 2024 20:25:14 +0800 Subject: [PATCH] fix: use okhttp3 to fix bad request error --- pom.xml | 5 + .../useblessingskin/shared/httpMethods.java | 133 ++++++------------ 2 files changed, 48 insertions(+), 90 deletions(-) diff --git a/pom.xml b/pom.xml index e791e1b..64af1e5 100644 --- a/pom.xml +++ b/pom.xml @@ -116,5 +116,10 @@ configurate-yaml 4.2.0-SNAPSHOT + + com.squareup.okhttp3 + okhttp + 4.12.0 + diff --git a/src/main/java/com/github/hank9999/useblessingskin/shared/httpMethods.java b/src/main/java/com/github/hank9999/useblessingskin/shared/httpMethods.java index b84981d..bcf071f 100644 --- a/src/main/java/com/github/hank9999/useblessingskin/shared/httpMethods.java +++ b/src/main/java/com/github/hank9999/useblessingskin/shared/httpMethods.java @@ -1,34 +1,33 @@ package com.github.hank9999.useblessingskin.shared; +import okhttp3.*; +import okhttp3.Response; + import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.*; -import java.net.HttpURLConnection; import java.net.URL; final public class httpMethods { + private static final OkHttpClient client = new OkHttpClient(); + public static String getUrl(String url) throws Exception { - URL obj = new URL(url); - HttpURLConnection con = (HttpURLConnection) obj.openConnection(); - con.setRequestMethod("GET"); - con.setRequestProperty("User-Agent", "UseBlessingSkinPlugin/1.0"); - con.setConnectTimeout(10000); - con.setReadTimeout(10000); - con.setDoOutput(true); - int responseCode = con.getResponseCode(); - if (responseCode == 404 || responseCode == 204) { - return null; - } - BufferedReader in = new BufferedReader( - new InputStreamReader(con.getInputStream())); - String inputLine; - StringBuilder response = new StringBuilder(); + Request request = new Request.Builder() + .url(url) + .header("User-Agent", "UseBlessingSkinPlugin/1.0") + .build(); - while ((inputLine = in.readLine()) != null) { - response.append(inputLine); + try (Response response = client.newCall(request).execute()) { + if (response.code() == 404 || response.code() == 204) { + return null; + } + + if (!response.isSuccessful()) { + throw new IOException("HTTP error code: " + response.code() + " | " + response.message()); + } + + return response.body() != null ? response.body().string() : null; } - in.close(); - return response.toString(); } public static boolean getPicture(String urlHttp, String path, String picName) throws Exception { @@ -54,75 +53,29 @@ public static boolean getPicture(String urlHttp, String path, String picName) th } public static String postPic(String urlHttp, String picName, String picPath) throws Exception { - - String BOUNDARY = "========7d4a6d158c9"; - - URL url = new URL(urlHttp); - HttpURLConnection conn = (HttpURLConnection) url.openConnection(); - - // 设置POST请求 - conn.setRequestMethod("POST"); - conn.setDoOutput(true); - conn.setDoInput(true); - conn.setUseCaches(false); - - // 设置header参数 - conn.setRequestProperty("User-Agent", "UseBlessingSkinPlugin/1.0"); - conn.setRequestProperty("connection", "Keep-Alive"); - conn.setRequestProperty("Charset", "UTF-8"); - conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY); - - OutputStream out = new DataOutputStream(conn.getOutputStream()); - - // 文件 - File file = new File(picPath); - - // 构建Body Header - String PostBody = "--" + - BOUNDARY + - "\r\n" + - "Content-Disposition: form-data; name=\"file\"; filename=\"" + picName + "\"" + "\r\n" + - "Content-Type: image/png" + - "\r\n" + - "\r\n"; - - // 将Body Header写入输出流 - out.write(PostBody.getBytes()); - - // 读取文件数据 - DataInputStream in = new DataInputStream(new FileInputStream( - file)); - - // 每次读1KB - byte[] bufferOut = new byte[1024]; - int bytes; - - // 写入输出流 - while ((bytes = in.read(bufferOut)) != -1) { - out.write(bufferOut, 0, bytes); + File filePath = new File(picPath); + MediaType mediaType = MediaType.parse("image/png"); + + // 构建请求体 + MultipartBody requestBody = new MultipartBody.Builder() + .setType(MultipartBody.FORM) + .addFormDataPart("file", picName, RequestBody.create(filePath, mediaType)) + .build(); + + // 构建请求 + Request request = new Request.Builder() + .url(urlHttp) + .post(requestBody) + .addHeader("User-Agent", "UseBlessingSkinPlugin/1.0") + .build(); + + // 执行请求并获取响应 + try (Response response = client.newCall(request).execute()) { + if (!response.isSuccessful()) { + throw new IOException("HTTP error code: " + response.code() + " | " + response.message()); + } + + return response.body() != null ? response.body().string() : "{}"; } - - // 添加换行 - out.write("\r\n".getBytes()); - in.close(); - - // 定义结尾数据分割线。 - byte[] end_data = ("\r\n" + "--" + BOUNDARY + "--" + "\r\n").getBytes(); - - // 添加结尾标识 - out.write(end_data); - out.flush(); - out.close(); - - // 定义BufferedReader输入流来读取URL的响应 - BufferedReader reader = new BufferedReader(new InputStreamReader( - conn.getInputStream())); - String line; - StringBuilder response = new StringBuilder(); - while ((line = reader.readLine()) != null) { - response.append(line); - } - - return response.toString(); } -} +} \ No newline at end of file