From 156c506f9354310164cab62e7cfbf73a668ff181 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BE=9E=E5=BA=90?= <109708109+CiiLu@users.noreply.github.com> Date: Sat, 4 Jul 2026 22:27:12 +0800 Subject: [PATCH] qwq --- .../jackhuang/hmcl/util/io/HttpRequest.java | 98 +++++++++++++------ 1 file changed, 69 insertions(+), 29 deletions(-) diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/HttpRequest.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/HttpRequest.java index 13d6854b197..8431d08ac8b 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/HttpRequest.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/HttpRequest.java @@ -131,78 +131,105 @@ public HttpURLConnection createConnection() throws IOException { return con; } - public static class HttpGetRequest extends HttpRequest { - protected HttpGetRequest(String url) { - super(url, "GET"); + protected void checkResponseCode(HttpURLConnection con) throws IOException { + int code = con.getResponseCode(); + if (code / 100 != 2) { + if (!ignoreHttpCode && !toleratedHttpCodes.contains(code)) { + try { + throw new ResponseCodeException(this.url, code, NetworkUtils.readFullyAsString(con)); + } catch (IOException e) { + throw new ResponseCodeException(this.url, code, e); + } + } + } + } + + public static abstract class HttpSimpleRequest extends HttpRequest { + protected HttpSimpleRequest(String url, String method) { + super(url, method); } + @Override public String getString() throws IOException { return getStringWithRetry(() -> { HttpURLConnection con = createConnection(); con = resolveConnection(con); - return IOUtils.readFullyAsString("gzip".equals(con.getContentEncoding()) ? IOUtils.wrapFromGZip(con.getInputStream()) : con.getInputStream()); + checkResponseCode(con); + + return IOUtils.readFullyAsString("gzip".equals(con.getContentEncoding()) + ? IOUtils.wrapFromGZip(con.getInputStream()) + : con.getInputStream()); }, retryTimes); } } - public static final class HttpPostRequest extends HttpRequest { - private byte[] bytes; + public static class HttpGetRequest extends HttpSimpleRequest { + protected HttpGetRequest(String url) { super(url, "GET"); } + } + + public static class HttpDeleteRequest extends HttpSimpleRequest { + protected HttpDeleteRequest(String url) { super(url, "DELETE"); } + } - private HttpPostRequest(String url) { - super(url, "POST"); + @SuppressWarnings("unchecked") + public static abstract class HttpEntityRequest> extends HttpRequest { + protected byte[] bytes; + + protected HttpEntityRequest(String url, String method) { + super(url, method); } - public HttpPostRequest contentType(String contentType) { + public T contentType(String contentType) { headers.put("Content-Type", contentType); - return this; + return (T) this; } - public HttpPostRequest json(Object payload) throws JsonParseException { + public T json(Object payload) throws JsonParseException { return string(payload instanceof String ? (String) payload : GSON.toJson(payload), "application/json"); } - public HttpPostRequest form(Map params) { + public T form(Map params) { return string(NetworkUtils.withQuery("", params), "application/x-www-form-urlencoded"); } @SafeVarargs - public final HttpPostRequest form(Pair... params) { + public final T form(Pair... params) { return form(mapOf(params)); } - public HttpPostRequest string(String payload, String contentType) { + public T string(String payload, String contentType) { bytes = payload.getBytes(UTF_8); - header("Content-Length", "" + bytes.length); + header("Content-Length", String.valueOf(bytes.length)); contentType(contentType + "; charset=utf-8"); - return this; + return (T) this; } + @Override public String getString() throws IOException { return getStringWithRetry(() -> { HttpURLConnection con = createConnection(); con.setDoOutput(true); - try (OutputStream os = con.getOutputStream()) { - os.write(bytes); - } - - URL url = new URL(this.url); - - if (con.getResponseCode() / 100 != 2) { - if (!ignoreHttpCode && !toleratedHttpCodes.contains(con.getResponseCode())) { - try { - throw new ResponseCodeException(url.toString(), con.getResponseCode(), NetworkUtils.readFullyAsString(con)); - } catch (IOException e) { - throw new ResponseCodeException(url.toString(), con.getResponseCode(), e); - } + if (bytes != null) { + try (OutputStream os = con.getOutputStream()) { + os.write(bytes); } } + checkResponseCode(con); return NetworkUtils.readFullyAsString(con); }, retryTimes); } } + public static final class HttpPostRequest extends HttpEntityRequest { + private HttpPostRequest(String url) { super(url, "POST"); } + } + + public static final class HttpPutRequest extends HttpEntityRequest { + private HttpPutRequest(String url) { super(url, "PUT"); } + } + public static HttpGetRequest GET(String url) { return new HttpGetRequest(url); } @@ -216,6 +243,19 @@ public static HttpPostRequest POST(String url) throws MalformedURLException { return new HttpPostRequest(url); } + public static HttpPutRequest PUT(String url) throws MalformedURLException { + return new HttpPutRequest(url); + } + + public static HttpDeleteRequest DELETE(String url) { + return new HttpDeleteRequest(url); + } + + @SafeVarargs + public static HttpDeleteRequest DELETE(String url, Pair... query) { + return DELETE(NetworkUtils.withQuery(url, mapOf(query))); + } + private static String getStringWithRetry(ExceptionalSupplier supplier, int retryTimes) throws IOException { Throwable exception = null; for (int i = 0; i < retryTimes; i++) {