|
@@ -0,0 +1,303 @@
|
|
|
+package com.tiangua.star.util;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.tiangua.kirin.api.exception.ServiceException;
|
|
|
+import org.apache.http.Consts;
|
|
|
+import org.apache.http.HttpEntity;
|
|
|
+import org.apache.http.HttpStatus;
|
|
|
+import org.apache.http.NameValuePair;
|
|
|
+import org.apache.http.client.config.RequestConfig;
|
|
|
+import org.apache.http.client.entity.UrlEncodedFormEntity;
|
|
|
+import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
+import org.apache.http.client.methods.HttpGet;
|
|
|
+import org.apache.http.client.methods.HttpPost;
|
|
|
+import org.apache.http.entity.StringEntity;
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
+import org.apache.http.message.BasicHeader;
|
|
|
+import org.apache.http.message.BasicNameValuePair;
|
|
|
+import org.apache.http.protocol.HTTP;
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+public class HttpClientThreeUtil {
|
|
|
+
|
|
|
+
|
|
|
+ private static CloseableHttpClient httpClient = HttpClients.createDefault();
|
|
|
+
|
|
|
+
|
|
|
+ public static RequestConfig HUIRONG_CONFIG = RequestConfig.custom()
|
|
|
+ .setConnectTimeout(3500)
|
|
|
+ .setConnectionRequestTimeout(1500)
|
|
|
+ .setSocketTimeout(3500)
|
|
|
+ .build();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * get
|
|
|
+ *
|
|
|
+ * @param url 请求的url
|
|
|
+ * @param queries 请求的参数,在浏览器?后面的数据,没有可以传null
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String get(String url, Map<String, String> queries) {
|
|
|
+
|
|
|
+ String responseBody = "";
|
|
|
+
|
|
|
+ StringBuilder sb = new StringBuilder(url);
|
|
|
+
|
|
|
+ if (queries != null && !queries.isEmpty()) {
|
|
|
+ boolean firstFlag = true;
|
|
|
+ for (Map.Entry<String, String> entry : queries.entrySet()) {
|
|
|
+ if (firstFlag) {
|
|
|
+ sb.append("?").append(entry.getKey()).append("=").append(entry.getValue());
|
|
|
+ firstFlag = false;
|
|
|
+ } else {
|
|
|
+ sb.append("&").append(entry.getKey()).append("=").append(entry.getValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ HttpGet httpGet = new HttpGet(sb.toString());
|
|
|
+
|
|
|
+ httpGet.setConfig(HUIRONG_CONFIG);
|
|
|
+
|
|
|
+ HttpEntity entity = null;
|
|
|
+
|
|
|
+ CloseableHttpResponse response = null;
|
|
|
+ try {
|
|
|
+ response = httpClient.execute(httpGet);
|
|
|
+ int status = response.getStatusLine().getStatusCode();
|
|
|
+ if (status == HttpStatus.SC_OK) {
|
|
|
+ entity = response.getEntity();
|
|
|
+ responseBody = EntityUtils.toString(entity);
|
|
|
+
|
|
|
+ }
|
|
|
+ } catch (Exception ex) {
|
|
|
+ ex.printStackTrace();
|
|
|
+ } finally {
|
|
|
+
|
|
|
+ if (entity != null) {
|
|
|
+ EntityUtils.consumeQuietly(entity);
|
|
|
+ }
|
|
|
+ if (response != null) {
|
|
|
+ try {
|
|
|
+ response.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return responseBody;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * post
|
|
|
+ *
|
|
|
+ * @param url 请求的url
|
|
|
+ * @param queries 请求的参数,在浏览器?后面的数据,没有可以传null
|
|
|
+ * @param params post form 提交的参数
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String post(String url, Map<String, String> queries, Map<String, String> params) {
|
|
|
+ String responseBody = "";
|
|
|
+
|
|
|
+ StringBuilder sb = new StringBuilder(url);
|
|
|
+
|
|
|
+ if (queries != null && !queries.isEmpty()) {
|
|
|
+ boolean firstFlag = true;
|
|
|
+ for (Map.Entry<String, String> entry : queries.entrySet()) {
|
|
|
+ if (firstFlag) {
|
|
|
+ sb.append("?").append(entry.getKey()).append("=").append(entry.getValue());
|
|
|
+ firstFlag = false;
|
|
|
+ } else {
|
|
|
+ sb.append("&").append(entry.getKey()).append("=").append(entry.getValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ HttpPost httpPost = new HttpPost(sb.toString());
|
|
|
+ httpPost.setConfig(HUIRONG_CONFIG);
|
|
|
+
|
|
|
+ List<NameValuePair> nvps = new ArrayList<>();
|
|
|
+ if (params != null && !params.isEmpty()) {
|
|
|
+ for (Map.Entry<String, String> entry : params.entrySet()) {
|
|
|
+ nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ httpPost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
|
|
|
+
|
|
|
+ HttpEntity entity = null;
|
|
|
+
|
|
|
+ CloseableHttpResponse response = null;
|
|
|
+ try {
|
|
|
+ response = httpClient.execute(httpPost);
|
|
|
+ if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
|
|
|
+ entity = response.getEntity();
|
|
|
+ responseBody = EntityUtils.toString(entity);
|
|
|
+
|
|
|
+ }
|
|
|
+ } catch (IOException ex) {
|
|
|
+ ex.printStackTrace();
|
|
|
+ } finally {
|
|
|
+
|
|
|
+ if (entity != null) {
|
|
|
+ EntityUtils.consumeQuietly(entity);
|
|
|
+ }
|
|
|
+ if (response != null) {
|
|
|
+ try {
|
|
|
+ response.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return responseBody;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * post
|
|
|
+ *
|
|
|
+ * @param url 请求的url
|
|
|
+ * @param data post json 提交的参数
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String post(String url, String data) {
|
|
|
+ String responseBody = "";
|
|
|
+
|
|
|
+
|
|
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
+ httpPost.setConfig(HUIRONG_CONFIG);
|
|
|
+
|
|
|
+ StringEntity se = new StringEntity(data, "utf-8");
|
|
|
+ se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
|
|
|
+ httpPost.setEntity(se);
|
|
|
+ se.setContentType("application/json");
|
|
|
+
|
|
|
+ HttpEntity entity = null;
|
|
|
+
|
|
|
+ CloseableHttpResponse response = null;
|
|
|
+ try {
|
|
|
+ response = httpClient.execute(httpPost);
|
|
|
+ if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
|
|
|
+ entity = response.getEntity();
|
|
|
+ responseBody = EntityUtils.toString(entity);
|
|
|
+ }else{
|
|
|
+ throw new ServiceException("接口调用异常:" + response.getStatusLine().getStatusCode() + " " + url);
|
|
|
+ }
|
|
|
+ } catch (IOException ex) {
|
|
|
+ ex.printStackTrace();
|
|
|
+ } finally {
|
|
|
+
|
|
|
+ if (entity != null) {
|
|
|
+ EntityUtils.consumeQuietly(entity);
|
|
|
+ }
|
|
|
+ if (response != null) {
|
|
|
+ try {
|
|
|
+ response.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return responseBody;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String post(String url, String data,String mchId) {
|
|
|
+ String responseBody = "";
|
|
|
+
|
|
|
+
|
|
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
+ httpPost.setConfig(HUIRONG_CONFIG);
|
|
|
+
|
|
|
+
|
|
|
+ StringEntity se = new StringEntity(data, "utf-8");
|
|
|
+ httpPost.addHeader("Content-Type", "text/xml");
|
|
|
+ httpPost.addHeader("User-Agent", "wxpay sdk java v1.0 " + mchId);
|
|
|
+ httpPost.setEntity(se);
|
|
|
+
|
|
|
+ HttpEntity entity = null;
|
|
|
+
|
|
|
+ CloseableHttpResponse response = null;
|
|
|
+ try {
|
|
|
+ response = httpClient.execute(httpPost);
|
|
|
+ if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
|
|
|
+ entity = response.getEntity();
|
|
|
+ responseBody = EntityUtils.toString(entity);
|
|
|
+ }
|
|
|
+ } catch (IOException ex) {
|
|
|
+ ex.printStackTrace();
|
|
|
+ } finally {
|
|
|
+
|
|
|
+ if (entity != null) {
|
|
|
+ EntityUtils.consumeQuietly(entity);
|
|
|
+ }
|
|
|
+ if (response != null) {
|
|
|
+ try {
|
|
|
+ response.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return responseBody;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * 发送HTTP POST请求 带头
|
|
|
+ * @param url
|
|
|
+ * @param headers
|
|
|
+ * @param requestBody
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static String sendHttpPostRequest(String url, Map<String, String> headers, JSONObject requestBody) throws Exception {
|
|
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
+
|
|
|
+
|
|
|
+ for (Map.Entry<String, String> entry : headers.entrySet()) {
|
|
|
+ httpPost.setHeader(entry.getKey(), entry.getValue());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ StringEntity requestEntity = new StringEntity(requestBody.toString(), "UTF-8");
|
|
|
+ requestEntity.setContentType("application/json");
|
|
|
+ httpPost.setEntity(requestEntity);
|
|
|
+
|
|
|
+
|
|
|
+ String responseBody = "";
|
|
|
+ CloseableHttpResponse response = null;
|
|
|
+ try {
|
|
|
+ response = httpClient.execute(httpPost);
|
|
|
+ int statusCode = response.getStatusLine().getStatusCode();
|
|
|
+ if (statusCode == HttpStatus.SC_OK) {
|
|
|
+ HttpEntity entity = response.getEntity();
|
|
|
+ responseBody = EntityUtils.toString(entity, "UTF-8");
|
|
|
+ } else {
|
|
|
+ throw new Exception("HTTP request failed with status code: " + statusCode);
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+
|
|
|
+ if (response != null) {
|
|
|
+ try {
|
|
|
+ response.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return responseBody;
|
|
|
+ }
|
|
|
+}
|