Repository navigation
Expand file tree
/
Copy pathRequests.java
More file actions
80 lines (61 loc) · 3.36 KB
/
Copy pathRequests.java
File metadata and controls
80 lines (61 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package chatapp;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Requests {
private static final HttpClient httpClient = HttpClient.newHttpClient();
public Requests() {
}
public static Response get(String url) throws IOException {
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(url)).GET().header("User-Agent", "Mozilla/5.0").build();
HttpResponse<String> response = null;
try {
response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return new Response(response.statusCode(), response.body(), null);
}
public static Response get(String url, String auth) throws IOException {
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(url)).GET().header("User-Agent", "Mozilla/5.0").header("Authorization", auth).build();
HttpResponse<String> response = null;
try {
response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return new Response(response.statusCode(), response.body(), null);
}
public static Response post(String url, String postParams, String auth) throws IOException {
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(url)).POST(HttpRequest.BodyPublishers.ofString(postParams)).header("User-Agent", "Mozilla/5.0").header("Content-Type", "application/json").header("Authorization", String.valueOf(auth)).build();
HttpResponse<String> response = null;
try {
response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return new Response(response.statusCode(), response.body(), null);
}
public static Response patch(String url, String patchParams, String auth) throws IOException {
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(url)).method("PATCH", HttpRequest.BodyPublishers.ofString(patchParams)).header("User-Agent", "Mozilla/5.0").header("Content-Type", "application/json").header("Authorization", String.valueOf(auth)).build();
HttpResponse<String> response = null;
try {
response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return new Response(response.statusCode(), response.body(), null);
}
public static Response delete(String url, String deleteParams, String auth) throws IOException {
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(url)).method("DELETE", HttpRequest.BodyPublishers.ofString(deleteParams)).header("User-Agent", "Mozilla/5.0").header("Authorization", String.valueOf(auth)).build();
HttpResponse<String> response = null;
try {
response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return new Response(response.statusCode(), response.body(), null);
}
}