[FEATURE] HttpClient in HttpRequest (LauncherCore)

This commit is contained in:
Gravita 2021-04-18 18:57:02 +07:00
parent 5d35c0823e
commit 54cb175ae7

View file

@ -2,14 +2,22 @@
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import pro.gravit.utils.helper.IOHelper;
import pro.gravit.utils.helper.LogHelper; import pro.gravit.utils.helper.LogHelper;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.net.http.HttpClient;
import java.net.http.HttpHeaders;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.time.Duration;
public final class HTTPRequest { public final class HTTPRequest {
private static final int TIMEOUT = 10000; private static final int TIMEOUT = 10000;
@ -22,35 +30,35 @@ public static JsonElement jsonRequest(JsonElement request, URL url) throws IOExc
} }
public static JsonElement jsonRequest(JsonElement request, String method, URL url) throws IOException { public static JsonElement jsonRequest(JsonElement request, String method, URL url) throws IOException {
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); HttpClient client = HttpClient.newBuilder()
connection.setDoInput(true); .build();
if (request != null) connection.setDoOutput(true); HttpRequest.BodyPublisher publisher;
connection.setRequestMethod(method); if(request != null) {
if (request != null) connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); publisher = HttpRequest.BodyPublishers.ofString(request.toString());
connection.setRequestProperty("Accept", "application/json"); } else {
if (TIMEOUT > 0) publisher = HttpRequest.BodyPublishers.noBody();
connection.setConnectTimeout(TIMEOUT); }
if (request != null)
try (OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), StandardCharsets.UTF_8)) {
writer.write(request.toString());
writer.flush();
}
InputStreamReader reader;
int statusCode = connection.getResponseCode();
if (200 <= statusCode && statusCode < 300)
reader = new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8);
else
reader = new InputStreamReader(connection.getErrorStream(), StandardCharsets.UTF_8);
try { try {
return JsonParser.parseReader(reader); HttpRequest request1 = HttpRequest.newBuilder()
} catch (Exception e) { .method(method, publisher)
if (200 > statusCode || statusCode > 300) { .uri(url.toURI())
LogHelper.error("JsonRequest failed. Server response code %d", statusCode); .header("Content-Type", "application/json; charset=UTF-8")
throw new IOException(e); .header("Accept", "application/json")
.timeout(Duration.ofMillis(TIMEOUT))
.build();
HttpResponse<InputStream> response = client.send(request1, HttpResponse.BodyHandlers.ofInputStream());
int statusCode = response.statusCode();
try {
return JsonParser.parseReader(IOHelper.newReader(response.body()));
} catch (Exception e) {
if (200 > statusCode || statusCode > 300) {
LogHelper.error("JsonRequest failed. Server response code %d", statusCode);
throw new IOException(e);
}
return null;
} }
return null; } catch (URISyntaxException | InterruptedException e) {
throw new IOException(e);
} }
} }
} }