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
14 package org.onap.msb.sdclient.wrapper.util;
16 import java.io.IOException;
17 import java.math.BigInteger;
18 import java.nio.charset.Charset;
19 import java.util.ArrayList;
20 import java.util.List;
22 import org.apache.http.NameValuePair;
23 import org.apache.http.client.ClientProtocolException;
24 import org.apache.http.client.config.RequestConfig;
25 import org.apache.http.client.methods.CloseableHttpResponse;
26 import org.apache.http.client.methods.HttpDelete;
27 import org.apache.http.client.methods.HttpGet;
28 import org.apache.http.client.methods.HttpPost;
29 import org.apache.http.client.utils.URLEncodedUtils;
30 import org.apache.http.entity.StringEntity;
31 import org.apache.http.impl.client.CloseableHttpClient;
32 import org.apache.http.impl.client.HttpClients;
33 import org.apache.http.message.BasicNameValuePair;
34 import org.apache.http.util.EntityUtils;
35 import org.onap.msb.sdclient.core.ConsulResponse;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 public class HttpClientUtil {
41 private static final Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);
43 public static int httpPostWithJSON(String url, String params) {
45 CloseableHttpClient httpClient = HttpClients.createDefault();
46 HttpPost httpPost = new HttpPost(url);
47 httpPost.addHeader("Content-type", "application/json; charset=utf-8");
48 httpPost.setHeader("Accept", "application/json");
49 httpPost.setEntity(new StringEntity(params, Charset.forName("UTF-8")));
51 CloseableHttpResponse res = httpClient.execute(httpPost);
52 result = res.getStatusLine().getStatusCode();
53 if (res.getStatusLine().getStatusCode() != 200) {
54 logger.error(String.valueOf(result));
57 } catch (IOException e) {
58 String errorMsg = url + ":httpPostWithJSON connect faild";
62 } catch (IOException e) {
63 String errorMsg = url + ":close httpClient faild";
71 public static void delete(String url, String parameter) throws Exception {
74 if (parameter != null) {
75 List<NameValuePair> params = new ArrayList<NameValuePair>();
76 params.add(new BasicNameValuePair("serviceName", parameter));
77 baseUrl = url + "?" + URLEncodedUtils.format(params, "UTF-8");
82 CloseableHttpClient httpClient = HttpClients.createDefault();;
85 HttpDelete httpDelete = new HttpDelete(baseUrl);
86 CloseableHttpResponse res = httpClient.execute(httpDelete);
88 if (res.getStatusLine().getStatusCode() != 200) {
89 throw new Exception("delete fail");
93 } catch (IOException e) {
94 String errorMsg = baseUrl + ":delete connect faild";
98 } catch (IOException e) {
99 String errorMsg = baseUrl + ":close httpClient faild";
105 public static String httpGet(String url) {
106 String result = null;
107 CloseableHttpClient httpClient = HttpClients.createDefault();
108 HttpGet httpGet = new HttpGet(url);
109 httpGet.addHeader("Content-type", "application/json; charset=utf-8");
110 httpGet.setHeader("Accept", "application/json");
112 CloseableHttpResponse res = httpClient.execute(httpGet);
114 res.getLastHeader("X-Consul-Index");
115 result = EntityUtils.toString(res.getEntity());
116 if (res.getStatusLine().getStatusCode() != 200) {
117 logger.error(result);
120 } catch (ClientProtocolException e) {
121 String errorMsg = url + ":httpGetWithJSON connect faild";
122 logger.error(errorMsg);
123 } catch (IOException e) {
124 String errorMsg = url + ":httpGetWithJSON connect faild";
125 logger.error(errorMsg);
129 } catch (IOException e) {
130 String errorMsg = url + ":close httpClient faild";
131 logger.error(errorMsg);
139 @SuppressWarnings("unchecked")
140 public static <T> ConsulResponse<T> httpWaitGet(String url) {
141 CloseableHttpClient httpClient = HttpClients.createDefault();
142 HttpGet httpGet = new HttpGet(url);
143 httpGet.addHeader("Content-type", "application/json; charset=utf-8");
144 httpGet.setHeader("Accept", "application/json");
146 CloseableHttpResponse res = httpClient.execute(httpGet);
147 String result = EntityUtils.toString(res.getEntity());
149 if (res.getStatusLine().getStatusCode() != 200) {
150 logger.error(result);
152 String indexHeaderValue = res.getLastHeader("X-Consul-Index").getValue();
153 BigInteger index = new BigInteger(indexHeaderValue);
155 return new ConsulResponse<T>((T) result, index);
160 } catch (ClientProtocolException e) {
161 String errorMsg = url + ":httpGetWithJSON connect faild " + e.getMessage();
162 logger.error(errorMsg);
163 } catch (IOException e) {
164 String errorMsg = url + ":httpGetWithJSON connect faild " + e.getMessage();
165 logger.error(errorMsg);
169 } catch (IOException e) {
170 String errorMsg = url + ":close httpClient faild " + e.getMessage();
171 logger.error(errorMsg);
179 public static int httpGetStatus(String url) throws Exception {
181 CloseableHttpClient httpClient = HttpClients.createDefault();
183 HttpGet httpGet = new HttpGet(url);
184 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(10000).build();// 设置请求和传输超时时间
185 httpGet.setConfig(requestConfig);
186 httpGet.addHeader("Content-type", "application/json; charset=utf-8");
187 httpGet.setHeader("Accept", "application/json");
189 CloseableHttpResponse res = httpClient.execute(httpGet);
191 iStatus = res.getStatusLine().getStatusCode();
193 } catch (ClientProtocolException e) {
194 logger.error(url + " httpGet connect faild:" + e.getMessage());
195 } catch (IOException e) {
196 logger.error(url + " httpGet connect faild:" + e.getMessage());
200 } catch (IOException e) {
201 logger.error(url + " httpGet close faild:" + e.getMessage());