-
Notifications
You must be signed in to change notification settings - Fork 53.6k
Expand file tree
/
Copy pathHttpClientRequestBuilder.java
More file actions
114 lines (96 loc) · 4.01 KB
/
HttpClientRequestBuilder.java
File metadata and controls
114 lines (96 loc) · 4.01 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
package com.baeldung.http;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class HttpClientRequestBuilder {
public HttpResponseWrapper sendGetRequest(String url, Map<String, String> parameters) throws UnsupportedEncodingException {
HttpClient client = HttpClientBuilder.create()
.build();
if (parameters != null) {
url += "?" + ParameterStringBuilder.getParamsString(parameters);
}
HttpGet request = new HttpGet(url);
try {
HttpResponse response = client.execute(request);
HttpResponseWrapper responseWrapper = new HttpResponseWrapper();
responseWrapper.setStatus(response.getStatusLine()
.getStatusCode());
BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity()
.getContent()));
String line, content = "";
while ((line = in.readLine()) != null) {
content += line;
}
responseWrapper.setContent(content);
return responseWrapper;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public HttpResponseWrapper sendPostRequestWithParameters(String url, Map<String, String> parameters) {
HttpClient client = HttpClientBuilder.create()
.build();
HttpPost request = new HttpPost(url);
try {
if (parameters != null) {
List<NameValuePair> nameValuePairs = parameters.entrySet().stream()
.map(e -> new BasicNameValuePair(e.getKey(), e.getValue()))
.collect(Collectors.toList());
request.setEntity(new UrlEncodedFormEntity(nameValuePairs));
}
HttpResponse response = client.execute(request);
HttpResponseWrapper responseWrapper = new HttpResponseWrapper();
responseWrapper.setStatus(response.getStatusLine()
.getStatusCode());
BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity()
.getContent()));
String line, content = "";
while ((line = in.readLine()) != null) {
content += line;
}
responseWrapper.setContent(content);
return responseWrapper;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public HttpResponseWrapper sendPostRequestWithJson(String url, String json) {
HttpClient client = HttpClientBuilder.create()
.build();
HttpPost request = new HttpPost(url);
try {
request.addHeader("Content-Type", "application/json");
request.setEntity(new StringEntity(json));
HttpResponse response = client.execute(request);
HttpResponseWrapper responseWrapper = new HttpResponseWrapper();
responseWrapper.setStatus(response.getStatusLine()
.getStatusCode());
BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity()
.getContent()));
String line, content = "";
while ((line = in.readLine()) != null) {
content += line;
}
responseWrapper.setContent(content);
return responseWrapper;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}