1 package org.onap.msb.apiroute.wrapper.util;
3 import java.io.IOException;
5 import org.apache.http.client.ClientProtocolException;
6 import org.apache.http.client.config.RequestConfig;
7 import org.apache.http.client.methods.CloseableHttpResponse;
8 import org.apache.http.client.methods.HttpGet;
9 import org.apache.http.impl.client.CloseableHttpClient;
10 import org.apache.http.impl.client.HttpClients;
11 import org.apache.http.util.EntityUtils;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
15 public class HttpClientUtil {
17 private static final Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);
19 private static int connectionTimeOut = 2*1000;
22 public static String httpGet(String url){
24 CloseableHttpClient httpClient = HttpClients.createDefault();
25 HttpGet httpGet = new HttpGet(url);
26 httpGet.addHeader("Content-type", "application/json; charset=utf-8");
27 httpGet.setHeader("Accept", "application/json");
29 CloseableHttpResponse res = httpClient.execute(httpGet);
30 result = EntityUtils.toString(res.getEntity());
31 if (res.getStatusLine().getStatusCode() != CommonUtil.SC_OK) {
35 } catch (ClientProtocolException e) {
36 logger.error(url + ":httpGetWithJSON connect faild");
37 } catch (IOException e) {
38 logger.error( url + ":httpGetWithJSON connect faild");
42 } catch (IOException e) {
43 logger.error(url + ":close httpClient faild");
51 public static HttpGetResult httpGetStatusAndBody(String url){
52 HttpGetResult result= new HttpGetResult();
54 CloseableHttpClient httpClient = HttpClients.createDefault();
55 HttpGet httpGet = new HttpGet(url);
56 httpGet.addHeader("Content-type", "application/json; charset=utf-8");
57 httpGet.setHeader("Accept", "application/json");
59 RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(connectionTimeOut).build();
60 httpGet.setConfig(requestConfig);
63 CloseableHttpResponse res = httpClient.execute(httpGet);
64 body = EntityUtils.toString(res.getEntity());
65 if (res.getStatusLine().getStatusCode() != CommonUtil.SC_OK) {
69 result.setStatusCode(res.getStatusLine().getStatusCode());
71 } catch (ClientProtocolException e) {
72 logger.error(url + ":httpGetWithJSON connect faild",e);
73 } catch (IOException e) {
74 logger.error( url + ":httpGetWithJSON connect faild",e);
78 } catch (IOException e) {
79 logger.error(url + ":close httpClient faild");
87 public static int httpGetStatus(String url) throws Exception{
89 CloseableHttpClient httpClient = HttpClients.createDefault();
92 HttpGet httpGet = new HttpGet(url);
93 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(10000).build();//设置请求和传输超时时间
94 httpGet.setConfig(requestConfig);
95 httpGet.addHeader("Content-type", "application/json; charset=utf-8");
96 httpGet.setHeader("Accept", "application/json");
98 CloseableHttpResponse res = httpClient.execute(httpGet);
100 iStatus=res.getStatusLine().getStatusCode();
102 } catch (ClientProtocolException e) {
103 logger.error(url + " httpGet connect faild:"+e.getMessage());
104 } catch (IOException e) {
105 logger.error(url + " httpGet connect faild:"+e.getMessage());
109 } catch (IOException e) {
110 logger.error(url + " httpGet close faild:"+e.getMessage());