|
|
@@ -0,0 +1,179 @@
|
|
|
+//package com.jisco.jwls.utils;
|
|
|
+//
|
|
|
+//import java.io.IOException;
|
|
|
+//import java.io.UnsupportedEncodingException;
|
|
|
+//import java.net.URI;
|
|
|
+//import java.net.URISyntaxException;
|
|
|
+//import java.nio.charset.Charset;
|
|
|
+//import java.util.ArrayList;
|
|
|
+//import java.util.Iterator;
|
|
|
+//import java.util.List;
|
|
|
+//import java.util.Map;
|
|
|
+//import java.util.Map.Entry;
|
|
|
+//import java.util.Set;
|
|
|
+//import java.util.concurrent.TimeUnit;
|
|
|
+//import org.apache.hc.client5.http.ClientProtocolException;
|
|
|
+//import org.apache.hc.client5.http.classic.methods.HttpGet;
|
|
|
+//import org.apache.hc.client5.http.classic.methods.HttpPost;
|
|
|
+//import org.apache.hc.client5.http.config.RequestConfig;
|
|
|
+//import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
|
|
|
+//import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
|
|
+//import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
|
|
|
+//import org.apache.hc.client5.http.impl.classic.HttpClients;
|
|
|
+//import org.apache.hc.core5.http.ContentType;
|
|
|
+//import org.apache.hc.core5.http.HttpEntity;
|
|
|
+//import org.apache.hc.core5.http.NameValuePair;
|
|
|
+//import org.apache.hc.core5.http.ParseException;
|
|
|
+//import org.apache.hc.core5.http.io.entity.EntityUtils;
|
|
|
+//import org.apache.hc.core5.http.io.entity.StringEntity;
|
|
|
+//import org.apache.hc.core5.http.message.BasicNameValuePair;
|
|
|
+//import org.apache.hc.core5.net.URIBuilder;
|
|
|
+//
|
|
|
+//import com.alibaba.fastjson.JSON;
|
|
|
+//
|
|
|
+//public class HttpUtil {
|
|
|
+//
|
|
|
+// public static String doGet(String url, Map<String, Object> paramMap, String token) {
|
|
|
+// CloseableHttpClient httpClient = null;
|
|
|
+// CloseableHttpResponse response = null;
|
|
|
+// String result = "";
|
|
|
+// try {
|
|
|
+// // 通过址默认配置创建一个httpClient实例
|
|
|
+// httpClient = HttpClients.createDefault();
|
|
|
+// URIBuilder uriBilder = new URIBuilder(url);
|
|
|
+// RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(10, TimeUnit.SECONDS)// 设置连接主机服务超时时间
|
|
|
+// .setConnectionRequestTimeout(10, TimeUnit.SECONDS)// 设置连接请求超时时间
|
|
|
+// .setResponseTimeout(10, TimeUnit.SECONDS)// 设置读取数据连接超时时间
|
|
|
+// .build();
|
|
|
+// URIBuilder uriBuilder = new URIBuilder(url);
|
|
|
+// if (null != paramMap && paramMap.size() > 0) {
|
|
|
+// List<NameValuePair> nvps = new ArrayList<NameValuePair>();
|
|
|
+// // 通过map集成entrySet方法获取entity
|
|
|
+// Set<Entry<String, Object>> entrySet = paramMap.entrySet();
|
|
|
+// // 循环遍历,获取迭代器
|
|
|
+// Iterator<Entry<String, Object>> iterator = entrySet.iterator();
|
|
|
+// while (iterator.hasNext()) {
|
|
|
+// Entry<String, Object> mapEntry = iterator.next();
|
|
|
+// nvps.add(new BasicNameValuePair(mapEntry.getKey(), mapEntry.getValue().toString()));
|
|
|
+// }
|
|
|
+// uriBilder.setParameters(nvps);
|
|
|
+//// httpGet.setEntity(new UrlEncodedFormEntity(nvps, Charset.forName("UTF-8")));
|
|
|
+// }
|
|
|
+// // 创建httpGet远程连接实例
|
|
|
+// HttpGet httpGet = new HttpGet(uriBilder.build());
|
|
|
+// // 为httpGet实例设置配置
|
|
|
+// httpGet.setConfig(requestConfig);
|
|
|
+// // 为httpPost实例设置配置
|
|
|
+// httpGet.setConfig(requestConfig);
|
|
|
+// httpGet.addHeader("contentType", "application/json; charset=UTF-8");
|
|
|
+// if(token != null) {
|
|
|
+// httpGet.addHeader("Authorization","bearer " + token);
|
|
|
+// }
|
|
|
+// httpGet.setHeader("Accept", "application/json");
|
|
|
+// response = httpClient.execute(httpGet);
|
|
|
+// // 通过返回对象获取返回数据
|
|
|
+// HttpEntity entity = response.getEntity();
|
|
|
+// // 通过EntityUtils中的toString方法将结果转换为字符串
|
|
|
+// result = EntityUtils.toString(entity);
|
|
|
+// } catch (ClientProtocolException e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// } catch (IOException e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// } catch (URISyntaxException e) {
|
|
|
+// // TODO Auto-generated catch block
|
|
|
+// e.printStackTrace();
|
|
|
+// } catch (ParseException e) {
|
|
|
+// // TODO Auto-generated catch block
|
|
|
+// e.printStackTrace();
|
|
|
+// } finally {
|
|
|
+// // 关闭资源
|
|
|
+// if (null != response) {
|
|
|
+// try {
|
|
|
+// response.close();
|
|
|
+// } catch (IOException e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+// }
|
|
|
+// if (null != httpClient) {
|
|
|
+// try {
|
|
|
+// httpClient.close();
|
|
|
+// } catch (IOException e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+// return result;
|
|
|
+// }
|
|
|
+//
|
|
|
+// public static String doPost(String url, Map<String, Object> paramMap, String token) {
|
|
|
+// CloseableHttpClient httpClient = null;
|
|
|
+// CloseableHttpResponse httpResponse = null;
|
|
|
+// String result = "";
|
|
|
+// // 创建httpClient实例
|
|
|
+// httpClient = HttpClients.createDefault();
|
|
|
+// // 创建httpPost远程连接实例
|
|
|
+// HttpPost httpPost = new HttpPost(url);
|
|
|
+// // 配置请求参数实例
|
|
|
+// RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(10, TimeUnit.SECONDS)// 设置连接主机服务超时时间
|
|
|
+// .setConnectionRequestTimeout(10, TimeUnit.SECONDS)// 设置连接请求超时时间
|
|
|
+// .setResponseTimeout(10, TimeUnit.SECONDS)// 设置读取数据连接超时时间
|
|
|
+// .build();
|
|
|
+// // 为httpPost实例设置配置
|
|
|
+// httpPost.setConfig(requestConfig);
|
|
|
+// httpPost.addHeader("contentType", "application/json; charset=UTF-8");
|
|
|
+// if(token != null) {
|
|
|
+// httpPost.addHeader("Authorization","bearer " + token);
|
|
|
+// }
|
|
|
+// httpPost.setHeader("Accept", "application/json");
|
|
|
+// // 封装post请求参数
|
|
|
+// if (null != paramMap && paramMap.size() > 0) {
|
|
|
+// List<NameValuePair> nvps = new ArrayList<NameValuePair>();
|
|
|
+// // 通过map集成entrySet方法获取entity
|
|
|
+// Set<Entry<String, Object>> entrySet = paramMap.entrySet();
|
|
|
+// // 循环遍历,获取迭代器
|
|
|
+// Iterator<Entry<String, Object>> iterator = entrySet.iterator();
|
|
|
+// while (iterator.hasNext()) {
|
|
|
+// Entry<String, Object> mapEntry = iterator.next();
|
|
|
+// nvps.add(new BasicNameValuePair(mapEntry.getKey(), mapEntry.getValue().toString()));
|
|
|
+// }
|
|
|
+// if(token == null) {
|
|
|
+// UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nvps, Charset.forName("UTF-8"));
|
|
|
+// httpPost.setEntity(entity);
|
|
|
+// } else {
|
|
|
+// StringEntity entity = new StringEntity(JSON.toJSONString(paramMap), ContentType.APPLICATION_JSON);
|
|
|
+// httpPost.setEntity(entity);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// try {
|
|
|
+// // httpClient对象执行post请求,并返回响应参数对象
|
|
|
+// httpResponse = httpClient.execute(httpPost);
|
|
|
+// // 从响应对象中获取响应内容
|
|
|
+// HttpEntity entity = httpResponse.getEntity();
|
|
|
+// result = EntityUtils.toString(entity);
|
|
|
+// } catch (ClientProtocolException e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// } catch (IOException e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// } catch (ParseException e) {
|
|
|
+// // TODO Auto-generated catch block
|
|
|
+// e.printStackTrace();
|
|
|
+// } finally {
|
|
|
+// // 关闭资源
|
|
|
+// if (null != httpResponse) {
|
|
|
+// try {
|
|
|
+// httpResponse.close();
|
|
|
+// } catch (IOException e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+// }
|
|
|
+// if (null != httpClient) {
|
|
|
+// try {
|
|
|
+// httpClient.close();
|
|
|
+// } catch (IOException e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+// return result;
|
|
|
+// }
|
|
|
+//}
|