1 /*******************************************************************************
2 * Copyright 2016-2017 ZTE, Inc. and others.
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
13 ******************************************************************************/
14 package org.onap.msb.apiroute.wrapper.util;
16 import java.io.IOException;
18 import org.apache.http.client.ClientProtocolException;
19 import org.apache.http.client.config.RequestConfig;
20 import org.apache.http.client.methods.CloseableHttpResponse;
21 import org.apache.http.client.methods.HttpGet;
22 import org.apache.http.impl.client.CloseableHttpClient;
23 import org.apache.http.impl.client.HttpClients;
24 import org.apache.http.util.EntityUtils;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 public class HttpClientUtil {
30 private static final Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);
32 private static int connectionTimeOut = 2 * 1000;
35 public static String httpGet(String url) {
37 CloseableHttpClient httpClient = HttpClients.createDefault();
38 HttpGet httpGet = new HttpGet(url);
39 httpGet.addHeader("Content-type", "application/json; charset=utf-8");
40 httpGet.setHeader("Accept", "application/json");
42 CloseableHttpResponse res = httpClient.execute(httpGet);
43 result = EntityUtils.toString(res.getEntity());
44 if (res.getStatusLine().getStatusCode() != CommonUtil.SC_OK) {
48 } catch (ClientProtocolException e) {
49 logger.error(url + ":httpGetWithJSON connect faild");
50 } catch (IOException e) {
51 logger.error(url + ":httpGetWithJSON connect faild");
55 } catch (IOException e) {
56 logger.error(url + ":close httpClient faild");
64 public static HttpGetResult httpGetStatusAndBody(String url) {
65 HttpGetResult result = new HttpGetResult();
67 CloseableHttpClient httpClient = HttpClients.createDefault();
68 HttpGet httpGet = new HttpGet(url);
69 httpGet.addHeader("Content-type", "application/json; charset=utf-8");
70 httpGet.setHeader("Accept", "application/json");
72 RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(connectionTimeOut).build();
73 httpGet.setConfig(requestConfig);
76 CloseableHttpResponse res = httpClient.execute(httpGet);
77 body = EntityUtils.toString(res.getEntity());
78 if (res.getStatusLine().getStatusCode() != CommonUtil.SC_OK) {
82 result.setStatusCode(res.getStatusLine().getStatusCode());
84 } catch (ClientProtocolException e) {
85 logger.error(url + ":httpGetWithJSON connect faild", e);
86 } catch (IOException e) {
87 logger.error(url + ":httpGetWithJSON connect faild", e);
91 } catch (IOException e) {
92 logger.error(url + ":close httpClient faild");
100 public static int httpGetStatus(String url) throws Exception {
102 CloseableHttpClient httpClient = HttpClients.createDefault();
105 HttpGet httpGet = new HttpGet(url);
106 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(10000).build();// 设置请求和传输超时时间
107 httpGet.setConfig(requestConfig);
108 httpGet.addHeader("Content-type", "application/json; charset=utf-8");
109 httpGet.setHeader("Accept", "application/json");
111 CloseableHttpResponse res = httpClient.execute(httpGet);
113 iStatus = res.getStatusLine().getStatusCode();
115 } catch (ClientProtocolException e) {
116 logger.error(url + " httpGet connect faild:" + e.getMessage());
117 } catch (IOException e) {
118 logger.error(url + " httpGet connect faild:" + e.getMessage());
122 } catch (IOException e) {
123 logger.error(url + " httpGet close faild:" + e.getMessage());