|
| 1 | +package top.mryan2005.simplifiedjava.requests; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 4 | +import top.mryan2005.simplifiedjava.Hash; |
| 5 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | +import java.net.URI; |
| 9 | +import java.net.http.HttpClient; |
| 10 | +import java.net.http.HttpRequest; |
| 11 | +import java.net.http.HttpResponse; |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.HashMap; |
| 14 | +import java.util.Map; |
| 15 | + |
| 16 | +public class POST { |
| 17 | + public Map headers = new HashMap<String, String>(); |
| 18 | + public Map body = new HashMap<String, String>(); |
| 19 | + public String URL; |
| 20 | + HttpClient client; |
| 21 | + public HttpRequest request; |
| 22 | + |
| 23 | + public POST(String url) { |
| 24 | + client = HttpClient.newHttpClient(); |
| 25 | + URL = url; |
| 26 | + } |
| 27 | + |
| 28 | + public void addHeader(String key, String value) { |
| 29 | + headers.put(key, value); |
| 30 | + } |
| 31 | + |
| 32 | + public void addBody(String key, String value) { |
| 33 | + body.put(key, value); |
| 34 | + } |
| 35 | + |
| 36 | + public void removeHeader(String key) { |
| 37 | + headers.remove(key); |
| 38 | + } |
| 39 | + |
| 40 | + public void removeItemInBody(String key) { |
| 41 | + body.remove(key); |
| 42 | + } |
| 43 | + |
| 44 | + public void clearHeaders() { |
| 45 | + headers.clear(); |
| 46 | + } |
| 47 | + |
| 48 | + public void clearBody() { |
| 49 | + body.clear(); |
| 50 | + } |
| 51 | + |
| 52 | + public HttpResponse<String> sendRequest() throws IOException, InterruptedException { |
| 53 | + var objectMapper = new ObjectMapper(); |
| 54 | + if(headers.size() == 0) { |
| 55 | + String requestBody = objectMapper |
| 56 | + .writeValueAsString(body); |
| 57 | + request= HttpRequest.newBuilder() |
| 58 | + .uri(URI.create(URL)) |
| 59 | + .POST(HttpRequest.BodyPublishers.ofString(requestBody)) |
| 60 | + .build(); |
| 61 | + return client.send(request, HttpResponse.BodyHandlers.ofString()); |
| 62 | + } |
| 63 | + if (body.size() == 0) { |
| 64 | + ArrayList<String> headerArray = new ArrayList<>(); |
| 65 | + for (Object key : this.headers.keySet()) { |
| 66 | + headerArray.add((String) key); |
| 67 | + headerArray.add((String) this.headers.get(key)); |
| 68 | + } |
| 69 | + request= HttpRequest.newBuilder() |
| 70 | + .uri(URI.create(URL)) |
| 71 | + .headers(headerArray.toArray(new String[0])) |
| 72 | + .POST(HttpRequest.BodyPublishers.noBody()) |
| 73 | + .build(); |
| 74 | + return client.send(request, HttpResponse.BodyHandlers.ofString()); |
| 75 | + } |
| 76 | + ArrayList<String> headerArray = new ArrayList<>(); |
| 77 | + for (Object key : this.headers.keySet()) { |
| 78 | + headerArray.add((String) key); |
| 79 | + headerArray.add((String) this.headers.get(key)); |
| 80 | + } |
| 81 | + request= HttpRequest.newBuilder() |
| 82 | + .uri(URI.create(URL)) |
| 83 | + .headers(headerArray.toArray(new String[0])) |
| 84 | + .POST(HttpRequest.BodyPublishers.ofString(objectMapper.writeValueAsString(body))) |
| 85 | + .build(); |
| 86 | + return client.send(request, HttpResponse.BodyHandlers.ofString()); |
| 87 | + } |
| 88 | + |
| 89 | + public HttpResponse<String> sendRequest(String url, Map headersInput, Map bodyInput) throws IOException, InterruptedException { |
| 90 | + var objectMapper = new ObjectMapper(); |
| 91 | + ArrayList<String> headerArray = new ArrayList<>(); |
| 92 | + for (Object key : headersInput.keySet()) { |
| 93 | + headerArray.add((String) key); |
| 94 | + headerArray.add((String) headersInput.get(key)); |
| 95 | + } |
| 96 | + request= HttpRequest.newBuilder() |
| 97 | + .uri(URI.create(url)) |
| 98 | + .headers(headerArray.toArray(new String[0])) |
| 99 | + .POST(HttpRequest.BodyPublishers.ofString(objectMapper.writeValueAsString(bodyInput))) |
| 100 | + .build(); |
| 101 | + return client.send(request, HttpResponse.BodyHandlers.ofString()); |
| 102 | + } |
| 103 | + |
| 104 | + public HttpResponse<String> sendRequest(String url, Map bodyInput) throws IOException, InterruptedException { |
| 105 | + var objectMapper = new ObjectMapper(); |
| 106 | + request= HttpRequest.newBuilder() |
| 107 | + .uri(URI.create(url)) |
| 108 | + .POST(HttpRequest.BodyPublishers.ofString(objectMapper.writeValueAsString(bodyInput))) |
| 109 | + .build(); |
| 110 | + return client.send(request, HttpResponse.BodyHandlers.ofString()); |
| 111 | + } |
| 112 | + |
| 113 | + public HttpResponse<String> sendRequest(String url) throws IOException, InterruptedException { |
| 114 | + request= HttpRequest.newBuilder() |
| 115 | + .uri(URI.create(url)) |
| 116 | + .POST(HttpRequest.BodyPublishers.noBody()) |
| 117 | + .build(); |
| 118 | + return client.send(request, HttpResponse.BodyHandlers.ofString()); |
| 119 | + } |
| 120 | + |
| 121 | + public static void main(String[] args) throws IOException, InterruptedException { |
| 122 | + POST post = new POST("https://httpbin.org/post"); |
| 123 | + post.addHeader("Content-Type", "application/json"); |
| 124 | + post.addBody("key", "value"); |
| 125 | + HttpResponse<String> res = post.sendRequest(); |
| 126 | + System.out.println(res.body()); |
| 127 | + } |
| 128 | +} |
0 commit comments