-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Samples.java
133 lines (110 loc) · 4.11 KB
/
Samples.java
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
import java.io.File;
import juno.http.FormBody;
import juno.http.HttpClient;
import juno.http.HttpRequest;
import juno.http.HttpResponse;
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()
.setDebug(true);
/*
GET https://postman-echo.com/get HTTP/1.1
*/
String get() throws Exception {
HttpRequest request = new 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
*/
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 = new 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"}
*/
String request() throws Exception {
String json = "{\"id\": \"7\", \"name\": \"bar\", \"active\": \"true\"}";
// application/json
RequestBody reqBody = RequestBody.create(
"application/json", json);
HttpRequest request = new 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 --
*/
String upload(File file) throws Exception {
// multipart/form-data
MultipartBody reqBody = new MultipartBody()
.addParam("name", "John Doe")
.addFile("file", file)
;
HttpRequest request = new 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
*/
File download() throws Exception {
HttpRequest request = new 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 = new HttpRequest("GET", url)
.addHeader("User-Agent", "nombre-cliente")
;
return client.execute(request);
}
public static void main(String[] args) throws Exception {
Samples samples = new Samples();
System.out.println(samples.get());
System.out.println(samples.post(7, "bar", true));
//
// File f = samples.download();
// System.out.println(f);
// System.out.println(samples.upload(f));
//
System.out.println(samples.request());
System.out.println(samples.getIpLocation().readString());
}
}