|
| 1 | +package com.github.lightlibs.simplehttpwrapper; |
| 2 | + |
| 3 | +import org.jetbrains.annotations.NotNull; |
| 4 | +import org.jetbrains.annotations.Nullable; |
| 5 | + |
| 6 | +import java.io.*; |
| 7 | +import java.net.HttpURLConnection; |
| 8 | +import java.net.URL; |
| 9 | +import java.nio.charset.StandardCharsets; |
| 10 | + |
| 11 | +public class SimpleHttpWrapper { |
| 12 | + |
| 13 | + public static SimpleHttpResponse get(@NotNull String urlStr, @Nullable String[] headers) throws IOException { |
| 14 | + return performRequest(SupportedHttpMethod.GET, urlStr, headers, null); |
| 15 | + } |
| 16 | + |
| 17 | + public static SimpleHttpResponse post(@NotNull String urlStr, @Nullable String[] headers, @Nullable String postData) throws IOException { |
| 18 | + return performRequest(SupportedHttpMethod.POST, urlStr, headers, postData); |
| 19 | + } |
| 20 | + |
| 21 | + public static SimpleHttpResponse performRequest(@NotNull SupportedHttpMethod method, String urlStr, @Nullable String[] headers, @Nullable String postData) throws IOException { |
| 22 | + return httpRequest(method.getRawMethodName(), urlStr, headers, postData); |
| 23 | + } |
| 24 | + |
| 25 | + @Deprecated |
| 26 | + public static SimpleHttpResponse performRequestUnsupported(@NotNull String method, String urlStr, @Nullable String[] headers, @Nullable String postData) throws IOException { |
| 27 | + return httpRequest(method, urlStr, headers, postData); |
| 28 | + } |
| 29 | + |
| 30 | + @NotNull |
| 31 | + private static SimpleHttpResponse httpRequest(@NotNull String method, String urlStr, @Nullable String[] headers, @Nullable String postData) throws IOException { |
| 32 | + URL url = new URL(urlStr); |
| 33 | + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
| 34 | + connection.setRequestMethod(method); |
| 35 | + |
| 36 | + if (headers != null) { |
| 37 | + for (String headerEntry : headers) { |
| 38 | + String[] parts = headerEntry.split(":"); |
| 39 | + if (parts.length != 2) continue; |
| 40 | + |
| 41 | + String key = parts[0]; |
| 42 | + String value = parts[1]; |
| 43 | + |
| 44 | + connection.setRequestProperty(key, value); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + if (method.equals("POST") && postData != null) { |
| 49 | + try (OutputStream os = connection.getOutputStream()) { |
| 50 | + byte[] postDataBytes = postData.getBytes(StandardCharsets.UTF_8); |
| 51 | + os.write(postDataBytes, 0, postDataBytes.length); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + int responseCode = connection.getResponseCode(); |
| 56 | + |
| 57 | + InputStream inputStream; |
| 58 | + if (responseCode >= 200 && responseCode < 300) inputStream = connection.getInputStream(); |
| 59 | + else inputStream = connection.getErrorStream(); |
| 60 | + |
| 61 | + BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); |
| 62 | + StringBuilder strb = new StringBuilder(); |
| 63 | + while (true) { |
| 64 | + String line = reader.readLine(); |
| 65 | + if (line == null) break; |
| 66 | + strb.append(line); |
| 67 | + } |
| 68 | + reader.close(); |
| 69 | + connection.disconnect(); |
| 70 | + |
| 71 | + return new SimpleHttpResponse(responseCode, strb.toString()); |
| 72 | + } |
| 73 | + |
| 74 | + public record SimpleHttpResponse(int statusCode, String data) {} |
| 75 | + |
| 76 | +} |
0 commit comments