Skip to content

Commit 5e621ce

Browse files
authored
HttpRequest 添加 PUTDELETE 请求支持 (#6312)
1 parent 1d1c8df commit 5e621ce

1 file changed

Lines changed: 69 additions & 29 deletions

File tree

HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/HttpRequest.java

Lines changed: 69 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -131,78 +131,105 @@ public HttpURLConnection createConnection() throws IOException {
131131
return con;
132132
}
133133

134-
public static class HttpGetRequest extends HttpRequest {
135-
protected HttpGetRequest(String url) {
136-
super(url, "GET");
134+
protected void checkResponseCode(HttpURLConnection con) throws IOException {
135+
int code = con.getResponseCode();
136+
if (code / 100 != 2) {
137+
if (!ignoreHttpCode && !toleratedHttpCodes.contains(code)) {
138+
try {
139+
throw new ResponseCodeException(this.url, code, NetworkUtils.readFullyAsString(con));
140+
} catch (IOException e) {
141+
throw new ResponseCodeException(this.url, code, e);
142+
}
143+
}
144+
}
145+
}
146+
147+
public static abstract class HttpSimpleRequest extends HttpRequest {
148+
protected HttpSimpleRequest(String url, String method) {
149+
super(url, method);
137150
}
138151

152+
@Override
139153
public String getString() throws IOException {
140154
return getStringWithRetry(() -> {
141155
HttpURLConnection con = createConnection();
142156
con = resolveConnection(con);
143-
return IOUtils.readFullyAsString("gzip".equals(con.getContentEncoding()) ? IOUtils.wrapFromGZip(con.getInputStream()) : con.getInputStream());
157+
checkResponseCode(con);
158+
159+
return IOUtils.readFullyAsString("gzip".equals(con.getContentEncoding())
160+
? IOUtils.wrapFromGZip(con.getInputStream())
161+
: con.getInputStream());
144162
}, retryTimes);
145163
}
146164
}
147165

148-
public static final class HttpPostRequest extends HttpRequest {
149-
private byte[] bytes;
166+
public static class HttpGetRequest extends HttpSimpleRequest {
167+
protected HttpGetRequest(String url) { super(url, "GET"); }
168+
}
169+
170+
public static class HttpDeleteRequest extends HttpSimpleRequest {
171+
protected HttpDeleteRequest(String url) { super(url, "DELETE"); }
172+
}
150173

151-
private HttpPostRequest(String url) {
152-
super(url, "POST");
174+
@SuppressWarnings("unchecked")
175+
public static abstract class HttpEntityRequest<T extends HttpEntityRequest<T>> extends HttpRequest {
176+
protected byte[] bytes;
177+
178+
protected HttpEntityRequest(String url, String method) {
179+
super(url, method);
153180
}
154181

155-
public HttpPostRequest contentType(String contentType) {
182+
public T contentType(String contentType) {
156183
headers.put("Content-Type", contentType);
157-
return this;
184+
return (T) this;
158185
}
159186

160-
public HttpPostRequest json(Object payload) throws JsonParseException {
187+
public T json(Object payload) throws JsonParseException {
161188
return string(payload instanceof String ? (String) payload : GSON.toJson(payload), "application/json");
162189
}
163190

164-
public HttpPostRequest form(Map<String, String> params) {
191+
public T form(Map<String, String> params) {
165192
return string(NetworkUtils.withQuery("", params), "application/x-www-form-urlencoded");
166193
}
167194

168195
@SafeVarargs
169-
public final HttpPostRequest form(Pair<String, String>... params) {
196+
public final T form(Pair<String, String>... params) {
170197
return form(mapOf(params));
171198
}
172199

173-
public HttpPostRequest string(String payload, String contentType) {
200+
public T string(String payload, String contentType) {
174201
bytes = payload.getBytes(UTF_8);
175-
header("Content-Length", "" + bytes.length);
202+
header("Content-Length", String.valueOf(bytes.length));
176203
contentType(contentType + "; charset=utf-8");
177-
return this;
204+
return (T) this;
178205
}
179206

207+
@Override
180208
public String getString() throws IOException {
181209
return getStringWithRetry(() -> {
182210
HttpURLConnection con = createConnection();
183211
con.setDoOutput(true);
184212

185-
try (OutputStream os = con.getOutputStream()) {
186-
os.write(bytes);
187-
}
188-
189-
URL url = new URL(this.url);
190-
191-
if (con.getResponseCode() / 100 != 2) {
192-
if (!ignoreHttpCode && !toleratedHttpCodes.contains(con.getResponseCode())) {
193-
try {
194-
throw new ResponseCodeException(url.toString(), con.getResponseCode(), NetworkUtils.readFullyAsString(con));
195-
} catch (IOException e) {
196-
throw new ResponseCodeException(url.toString(), con.getResponseCode(), e);
197-
}
213+
if (bytes != null) {
214+
try (OutputStream os = con.getOutputStream()) {
215+
os.write(bytes);
198216
}
199217
}
200218

219+
checkResponseCode(con);
201220
return NetworkUtils.readFullyAsString(con);
202221
}, retryTimes);
203222
}
204223
}
205224

225+
public static final class HttpPostRequest extends HttpEntityRequest<HttpPostRequest> {
226+
private HttpPostRequest(String url) { super(url, "POST"); }
227+
}
228+
229+
public static final class HttpPutRequest extends HttpEntityRequest<HttpPutRequest> {
230+
private HttpPutRequest(String url) { super(url, "PUT"); }
231+
}
232+
206233
public static HttpGetRequest GET(String url) {
207234
return new HttpGetRequest(url);
208235
}
@@ -216,6 +243,19 @@ public static HttpPostRequest POST(String url) throws MalformedURLException {
216243
return new HttpPostRequest(url);
217244
}
218245

246+
public static HttpPutRequest PUT(String url) throws MalformedURLException {
247+
return new HttpPutRequest(url);
248+
}
249+
250+
public static HttpDeleteRequest DELETE(String url) {
251+
return new HttpDeleteRequest(url);
252+
}
253+
254+
@SafeVarargs
255+
public static HttpDeleteRequest DELETE(String url, Pair<String, String>... query) {
256+
return DELETE(NetworkUtils.withQuery(url, mapOf(query)));
257+
}
258+
219259
private static String getStringWithRetry(ExceptionalSupplier<String, IOException> supplier, int retryTimes) throws IOException {
220260
Throwable exception = null;
221261
for (int i = 0; i < retryTimes; i++) {

0 commit comments

Comments
 (0)