blob: eb5ed1ee2da926b1716eb6ed090fad60dca99fa6 (
plain)
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
|
package org.onap.msb.apiroute.wrapper.util;
import java.io.IOException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HttpClientUtil {
private static final Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);
private static int connectionTimeOut = 2*1000;
public static String httpGet(String url){
String result = null;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
httpGet.addHeader("Content-type", "application/json; charset=utf-8");
httpGet.setHeader("Accept", "application/json");
try {
CloseableHttpResponse res = httpClient.execute(httpGet);
result = EntityUtils.toString(res.getEntity());
if (res.getStatusLine().getStatusCode() != CommonUtil.SC_OK) {
logger.error(result);
}
res.close();
} catch (ClientProtocolException e) {
logger.error(url + ":httpGetWithJSON connect faild");
} catch (IOException e) {
logger.error( url + ":httpGetWithJSON connect faild");
} finally {
try {
httpClient.close();
} catch (IOException e) {
logger.error(url + ":close httpClient faild");
}
}
return result;
}
public static HttpGetResult httpGetStatusAndBody(String url){
HttpGetResult result= new HttpGetResult();
String body = null;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
httpGet.addHeader("Content-type", "application/json; charset=utf-8");
httpGet.setHeader("Accept", "application/json");
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(connectionTimeOut).build();
httpGet.setConfig(requestConfig);
try {
CloseableHttpResponse res = httpClient.execute(httpGet);
body = EntityUtils.toString(res.getEntity());
if (res.getStatusLine().getStatusCode() != CommonUtil.SC_OK) {
logger.error(body);
}
result.setBody(body);
result.setStatusCode(res.getStatusLine().getStatusCode());
res.close();
} catch (ClientProtocolException e) {
logger.error(url + ":httpGetWithJSON connect faild",e);
} catch (IOException e) {
logger.error( url + ":httpGetWithJSON connect faild",e);
} finally {
try {
httpClient.close();
} catch (IOException e) {
logger.error(url + ":close httpClient faild");
}
}
return result;
}
public static int httpGetStatus(String url) throws Exception{
int iStatus=500;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(10000).build();//设置请求和传输超时时间
httpGet.setConfig(requestConfig);
httpGet.addHeader("Content-type", "application/json; charset=utf-8");
httpGet.setHeader("Accept", "application/json");
try {
CloseableHttpResponse res = httpClient.execute(httpGet);
iStatus=res.getStatusLine().getStatusCode();
res.close();
} catch (ClientProtocolException e) {
logger.error(url + " httpGet connect faild:"+e.getMessage());
} catch (IOException e) {
logger.error(url + " httpGet connect faild:"+e.getMessage());
} finally {
try {
httpClient.close();
} catch (IOException e) {
logger.error(url + " httpGet close faild:"+e.getMessage());
}
}
return iStatus;
}
}
|