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;
17 import java.nio.charset.Charset;
18 import java.util.ArrayList;
19 import java.util.List;
21 import org.apache.http.NameValuePair;
22 import org.apache.http.client.ClientProtocolException;
23 import org.apache.http.client.config.RequestConfig;
24 import org.apache.http.client.methods.CloseableHttpResponse;
25 import org.apache.http.client.methods.HttpDelete;
26 import org.apache.http.client.methods.HttpGet;
27 import org.apache.http.client.methods.HttpPost;
28 import org.apache.http.client.utils.URLEncodedUtils;
29 import org.apache.http.entity.StringEntity;
30 import org.apache.http.impl.client.CloseableHttpClient;
31 import org.apache.http.impl.client.HttpClients;
32 import org.apache.http.message.BasicNameValuePair;
33 import org.apache.http.util.EntityUtils;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 public class HttpClientUtil {
39 private static final Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);
41 private static int connectionTimeOut = 2 * 1000;
43 public static HttpPost createHttpPost(String url, String params){
44 HttpPost httpPost = new HttpPost(url);
45 httpPost.addHeader("Content-type", "application/json; charset=utf-8");
46 httpPost.setHeader("Accept", "application/json");
47 httpPost.setEntity(new StringEntity(params, Charset.forName("UTF-8")));
51 public static HttpGet createHttpGet(String url){
52 HttpGet httpGet = new HttpGet(url);
53 httpGet.addHeader("Content-type", "application/json; charset=utf-8");
54 httpGet.setHeader("Accept", "application/json");
58 public static void closeHttpClient(CloseableHttpClient httpClient) {
59 if (httpClient != null) {
62 } catch (IOException e) {
63 logger.error(httpClient + ":close httpClient faild");
68 public static void closeHttpResponse(CloseableHttpResponse response) {
69 if (response != null) {
72 } catch (IOException e) {
73 logger.error(response + ":close response faild");
78 public static CloseableHttpResponse httpGetWithResponse(String url) throws Exception {
79 CloseableHttpClient httpClient = HttpClients.createDefault();
80 HttpGet httpGet = new HttpGet(url);
81 httpGet.addHeader("Content-type", "application/json; charset=utf-8");
82 httpGet.setHeader("Accept", "application/json");
84 return httpClient.execute(httpGet);
88 } catch (IOException e) {
89 logger.error(url + ":close httpClient faild");
94 public static void delete(String url, String parameter) throws Exception {
97 if (parameter != null) {
98 List<NameValuePair> params = new ArrayList<NameValuePair>();
99 params.add(new BasicNameValuePair("serviceName", parameter));
100 baseUrl = url + "?" + URLEncodedUtils.format(params, "UTF-8");
105 CloseableHttpClient httpClient = HttpClients.createDefault();;
108 HttpDelete httpDelete = new HttpDelete(baseUrl);
109 CloseableHttpResponse res = httpClient.execute(httpDelete);
111 if (res.getStatusLine().getStatusCode() != 200) {
112 throw new Exception("delete fail");
116 } catch (IOException e) {
117 String errorMsg = baseUrl + ":delete connect faild";
121 } catch (IOException e) {
122 String errorMsg = baseUrl + ":close httpClient faild";
129 public static String httpGet(String url) {
130 String result = null;
131 CloseableHttpClient httpClient = HttpClients.createDefault();
132 HttpGet httpGet = new HttpGet(url);
133 httpGet.addHeader("Content-type", "application/json; charset=utf-8");
134 httpGet.setHeader("Accept", "application/json");
136 CloseableHttpResponse res = httpClient.execute(httpGet);
137 result = EntityUtils.toString(res.getEntity());
138 if (res.getStatusLine().getStatusCode() != CommonUtil.SC_OK) {
139 logger.error(result);
142 } catch (ClientProtocolException e) {
143 logger.error(url + ":httpGetWithJSON connect faild");
144 } catch (IOException e) {
145 logger.error(url + ":httpGetWithJSON connect faild");
149 } catch (IOException e) {
150 logger.error(url + ":close httpClient faild");
158 public static HttpGetResult httpGetStatusAndBody(String url) {
159 HttpGetResult result = new HttpGetResult();
161 CloseableHttpClient httpClient = HttpClients.createDefault();
162 HttpGet httpGet = new HttpGet(url);
163 httpGet.addHeader("Content-type", "application/json; charset=utf-8");
164 httpGet.setHeader("Accept", "application/json");
166 RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(connectionTimeOut).build();
167 httpGet.setConfig(requestConfig);
170 CloseableHttpResponse res = httpClient.execute(httpGet);
171 body = EntityUtils.toString(res.getEntity());
172 if (res.getStatusLine().getStatusCode() != CommonUtil.SC_OK) {
175 result.setBody(body);
176 result.setStatusCode(res.getStatusLine().getStatusCode());
178 } catch (ClientProtocolException e) {
179 logger.error(url + ":httpGetWithJSON connect faild", e);
180 } catch (IOException e) {
181 logger.error(url + ":httpGetWithJSON connect faild", e);
185 } catch (IOException e) {
186 logger.error(url + ":close httpClient faild");
194 public static int httpGetStatus(String url) throws Exception {
196 CloseableHttpClient httpClient = HttpClients.createDefault();
199 HttpGet httpGet = new HttpGet(url);
200 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(10000).build();// 设置请求和传输超时时间
201 httpGet.setConfig(requestConfig);
202 httpGet.addHeader("Content-type", "application/json; charset=utf-8");
203 httpGet.setHeader("Accept", "application/json");
205 CloseableHttpResponse res = httpClient.execute(httpGet);
207 iStatus = res.getStatusLine().getStatusCode();
209 } catch (ClientProtocolException e) {
210 logger.error(url + " httpGet connect faild:" + e.getMessage());
211 } catch (IOException e) {
212 logger.error(url + " httpGet connect faild:" + e.getMessage());
216 } catch (IOException e) {
217 logger.error(url + " httpGet close faild:" + e.getMessage());