Java使用RestfulApi:POST

Java使用RestfulApi-POST

pom.xml設定

1
2
3
4
5
6
<!-- apache http -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>

post method-1

get方法,傳入uri、params、headers、retry、timeout參數。

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
public static Optional<String> post(String uri, Map<String, String> params, List<Header> headers, int retry,
int timeout) throws Exception {

for (int i = retry; i > 0; i--) {
try {
HttpPostRequest request = ApacheHttpClientManager.getInstance().getHttpPostRequest(uri);
request.setParameters(params);
if (headers != null) {
request.setHeaders(headers);
}
request.setTimeout(timeout);
HTTPResponse httPResponse = ApacheHttpClientManager.getInstance().execute(request);
return Optional.ofNullable(httPResponse.getContent());

} catch (Exception e) {
if (1 == i) {
LogUtils.error.error(e.getMessage());
throw e;
}
continue;
}
}

return Optional.empty();
}

post method-2

get方法,傳入uri、jsonString、headers、retry、timeout參數。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public static Optional<String> post(String uri, String jsonString, List<Header> headers, int retry, int timeout)
throws Exception {

for (int i = retry; i > 0; i--) {
try {
HttpPostRequest request = ApacheHttpClientManager.getInstance().getHttpPostRequest(uri);
request.setStringEntity(jsonString);
if (headers != null) {
request.setHeaders(headers);
}
request.setTimeout(timeout);
HTTPResponse httPResponse = ApacheHttpClientManager.getInstance().execute(request);
return Optional.ofNullable(httPResponse.getContent());

} catch (Exception e) {
if (1 == i) {
LogUtils.error.error(e.getMessage());
throw e;
}
continue;
}
}
return Optional.empty();
}

ApacheHttpClientManager

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
public HttpPostRequest getHttpPostRequest(String url) throws Exception {
return new HttpPostRequest(url);
}

public HTTPResponse execute(HttpPostRequest request) throws Exception {
String url = request.getUrl();
Header[] headers = request.getHeaders();
HttpEntity httpEntity = request.getHttpEntity();
HttpClientContext proxyHttpContext = request.getHttpContext();

HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Accept-Charset", "UTF-8");
httpPost.setProtocolVersion(HttpVersion.HTTP_1_1);
if (headers != null && headers.length > 0) {
httpPost.setHeaders(headers);
}

if (httpEntity != null) {
httpPost.setEntity(httpEntity);
}

RequestConfig requestConfig = request.getRequestConfig();

if (requestConfig != null) {
httpPost.setConfig(requestConfig);
}

CloseableHttpResponse response = null;
String content = null;
Header[] responseHeaders = null;
int statusCode = -1;
CookieStore cookieStore = null;
try {
if (proxyHttpContext != null) {
response = httpClient.execute(httpPost, proxyHttpContext);
if (request.receiveCookie()) {
cookieStore = proxyHttpContext.getCookieStore();
}
} else {
if (request.receiveCookie()) {
HttpClientContext httpClientContext = HttpClientContext.create();
response = httpClient.execute(httpPost, httpClientContext);
cookieStore = httpClientContext.getCookieStore();
} else {
response = httpClient.execute(httpPost);
}
}
responseHeaders = response.getAllHeaders();
statusCode = response.getStatusLine().getStatusCode();

if (statusCode != 200 && statusCode != 404) {
log.info("HTTP Status-Code (" + statusCode + ") Length w/o headers: [" + url.length() + "] "
+ url);
}

HttpEntity entity = response.getEntity();

if (entity != null) {
content = getResponse(entity);
}

} catch (Exception e) {
log.error(e.getMessage(), e);
httpPost.abort();
throw e;
} finally {
close(response);
if (httpPost != null) {
httpPost.releaseConnection();
}
}
return new HTTPResponse(statusCode, content, responseHeaders,
(cookieStore == null ? null : cookieStore.getCookies()));
}