-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSamples.java
More file actions
136 lines (113 loc) · 4.28 KB
/
Copy pathSamples.java
File metadata and controls
136 lines (113 loc) · 4.28 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import java.io.File;
import juno.http.FormBody;
import juno.http.HttpClient;
import juno.http.HttpRequest;
import juno.http.HttpResponse;
import juno.http.HttpResult;
import juno.http.HttpUrl;
import juno.http.MultipartBody;
import juno.http.RequestBody;
import juno.http.convert.generic.FileResponseBodyConverter;
public class Samples {
HttpClient client = HttpClient.getInstance()
.addHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/149.0.0.0 Safari/537.36")
//.setDebug(true)
;
/*
GET https://postman-echo.com/get HTTP/1.1
*/
HttpResult<String> get() throws Exception {
HttpRequest request = HttpRequest.get(
"https://postman-echo.com/get");
return client.execute(request, String.class);
}
/*
POST https://postman-echo.com/post HTTP/1.1
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Content-Length: 25
id=7&name=bar&active=true
*/
HttpResult<String> post(int id, String name, boolean active) throws Exception {
// application-www-www-form-urlencoded
FormBody reqBody = new FormBody()
.add("id", id)
.add("name", name)
.add("active", active)
;
HttpRequest request = HttpRequest.post(
"https://postman-echo.com/post", reqBody);
return client.execute(request, String.class);
}
/*
POST https://postman-echo.com/post HTTP/1.1
Content-Type: application/json; charset=UTF-8
Content-Length: 44
{"id": "7", "name": "bar", "active": "true"}
*/
HttpResult<String> request() throws Exception {
String json = "{\"id\": \"7\", \"name\": \"bar\", \"active\": \"true\"}";
// application/json
RequestBody reqBody = RequestBody.create(
"application/json", json);
HttpRequest request = HttpRequest.post(
"https://postman-echo.com/post", reqBody);
return client.execute(request, String.class);
}
/*
POST https://postman-echo.com/post HTTP/1.1
Content-Type: multipart/form-data; boundary=30704407372601
Content-Length: 73152
-- binary --
*/
HttpResult<String> upload(File file) throws Exception {
// multipart/form-data
MultipartBody reqBody = new MultipartBody()
.addParam("name", "John Doe")
.addFile("file", file)
;
HttpRequest request = HttpRequest.post(
"https://postman-echo.com/post", reqBody);
return client.execute(request, String.class);
}
/*
GET https://github.com/jesusbmx/java-http-client/raw/master/dist/juno-http-client.jar HTTP/1.1
*/
HttpResult<File> download() throws Exception {
HttpRequest request = HttpRequest.get(
"https://github.com/jesusbmx/java-http-client/raw/master/dist/juno-http-client.jar")
.setTimeoutMs(20000);
FileResponseBodyConverter convert = new FileResponseBodyConverter()
.setDir(System.getProperty("user.home") + "\\Downloads\\") //.setName("httpclient.jar")
;
return client.execute(request, convert);
//return client.execute(request, File.class);
}
/*
GET http://ip-api.com/json/24.48.0.1?fields=status%2Cmessage%2Cquery%2Ccountry%2Ccity&lang=en HTTP/1.1
User-Agent: nombre-cliente
*/
HttpResponse getIpLocation() throws Exception {
HttpUrl url = new HttpUrl("http://ip-api.com/")
.addPath("json")
.addPath("24.48.0.1")
.addQueryParameter("fields", "status,message,query,country,city")
.addQueryParameter("lang", "en")
;
HttpRequest request = HttpRequest.get(url)
.addHeader("User-Agent", "nombre-cliente")
;
return client.send(request);
}
public static void main(String[] args) throws Exception {
Samples samples = new Samples();
System.out.println(samples.get().data);
System.out.println(samples.post(7, "bar", true).data);
//
// File f = samples.download().data;
// System.out.println(f);
// System.out.println(samples.upload(f));
//
System.out.println(samples.request().data);
System.out.println(samples.getIpLocation().readString());
}
}