Skip to content

Commit 42bbdeb

Browse files
committed
add GET and Post
1 parent 64f4f75 commit 42bbdeb

File tree

4 files changed

+217
-1
lines changed

4 files changed

+217
-1
lines changed

pom.xml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,22 @@
8080
<version>4.13.2</version>
8181
<scope>test</scope>
8282
</dependency>
83+
<!--Jackson包-->
84+
<dependency>
85+
<groupId>com.fasterxml.jackson.core</groupId>
86+
<artifactId>jackson-core</artifactId>
87+
<version>2.9.0</version>
88+
</dependency>
89+
<dependency>
90+
<groupId>com.fasterxml.jackson.core</groupId>
91+
<artifactId>jackson-databind</artifactId>
92+
<version>2.9.0</version>
93+
</dependency>
94+
<dependency>
95+
<groupId>com.fasterxml.jackson.core</groupId>
96+
<artifactId>jackson-annotations</artifactId>
97+
<version>2.9.0</version>
98+
</dependency>
8399
</dependencies>
84100

85101
<build>
@@ -157,7 +173,6 @@
157173
<configuration>
158174
<altDeploymentRepository>github::default::https://maven.pkg.github.com/Mryan2005/SimplifiedJava</altDeploymentRepository>
159175
</configuration>
160-
161176
</plugin>
162177
</plugins>
163178
</build>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 GET {
17+
public Map headers = new HashMap<String, String>();
18+
public String URL;
19+
HttpClient client;
20+
public HttpRequest request;
21+
22+
public GET(String url) {
23+
client = HttpClient.newHttpClient();
24+
URL = url;
25+
}
26+
27+
public void addHeader(String key, String value) {
28+
headers.put(key, value);
29+
}
30+
31+
public void removeHeader(String key) {
32+
headers.remove(key);
33+
}
34+
35+
public void clearHeaders() {
36+
headers.clear();
37+
}
38+
39+
public HttpResponse<String> sendRequest() throws IOException, InterruptedException {
40+
ArrayList<String> headerArray = new ArrayList<>();
41+
for (Object key : this.headers.keySet()) {
42+
headerArray.add((String) key);
43+
headerArray.add((String) this.headers.get(key));
44+
}
45+
request= HttpRequest.newBuilder()
46+
.uri(URI.create(URL))
47+
.headers(headerArray.toArray(new String[0]))
48+
.GET()
49+
.build();
50+
return client.send(request, HttpResponse.BodyHandlers.ofString());
51+
}
52+
53+
public HttpResponse<String> sendRequest(String url, Map headersInput) throws IOException, InterruptedException {
54+
ArrayList<String> headerArray = new ArrayList<>();
55+
for (Object key : headersInput.keySet()) {
56+
headerArray.add((String) key);
57+
headerArray.add((String) headersInput.get(key));
58+
}
59+
request= HttpRequest.newBuilder()
60+
.uri(URI.create(URL))
61+
.headers(headerArray.toArray(new String[0]))
62+
.GET()
63+
.build();
64+
return client.send(request, HttpResponse.BodyHandlers.ofString());
65+
}
66+
67+
public static void main(String[] args) throws IOException, InterruptedException {
68+
GET get = new GET("https://httpbin.org/GET");
69+
get.addHeader("Content-Type", "application/json");
70+
HttpResponse<String> res = get.sendRequest();
71+
System.out.println(res.body());
72+
}
73+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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

Comments
 (0)