|
@@ -0,0 +1,256 @@
|
|
|
|
+package cn.com.qmth.ac.util;
|
|
|
|
+
|
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.io.InputStream;
|
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
|
+import java.net.URLEncoder;
|
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
+import java.util.Objects;
|
|
|
|
+
|
|
|
|
+import org.apache.http.HttpEntity;
|
|
|
|
+import org.apache.http.HttpResponse;
|
|
|
|
+import org.apache.http.client.config.RequestConfig;
|
|
|
|
+import org.apache.http.client.entity.UrlEncodedFormEntity;
|
|
|
|
+import org.apache.http.client.methods.HttpGet;
|
|
|
|
+import org.apache.http.client.methods.HttpPost;
|
|
|
|
+import org.apache.http.client.methods.HttpUriRequest;
|
|
|
|
+import org.apache.http.entity.StringEntity;
|
|
|
|
+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 org.apache.tomcat.util.http.fileupload.IOUtils;
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
+
|
|
|
|
+import cn.com.qmth.ac.bean.SystemConstant;
|
|
|
|
+
|
|
|
|
+public class HttpUtil {
|
|
|
|
+
|
|
|
|
+ private final static Logger log = LoggerFactory.getLogger(HttpUtil.class);
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * post json
|
|
|
|
+ *
|
|
|
|
+ * @param url
|
|
|
|
+ * @param json
|
|
|
|
+ * @param secret
|
|
|
|
+ * @param timestamp
|
|
|
|
+ * @return
|
|
|
|
+ * @throws IOException
|
|
|
|
+ */
|
|
|
|
+ public static String postJson(String url, String json, String secret, Long timestamp) throws IOException {
|
|
|
|
+ RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(SystemConstant.CONNECT_TIME_OUT)// 连接主机服务超时时间
|
|
|
|
+ .setConnectionRequestTimeout(SystemConstant.CONNECT_TIME_OUT)// 请求超时时间
|
|
|
|
+ .setSocketTimeout(SystemConstant.SOCKET_CONNECT_TIME_OUT)// 数据读取超时时间
|
|
|
|
+ .build();
|
|
|
|
+
|
|
|
|
+ // 构建post请求
|
|
|
|
+ HttpPost post = new HttpPost(url);
|
|
|
|
+ post.setConfig(requestConfig);
|
|
|
|
+ post.setHeader(SystemConstant.HEADER_AUTHORIZATION, secret);
|
|
|
|
+ post.setHeader(SystemConstant.HEADER_TIME, String.valueOf(timestamp));
|
|
|
|
+ post.setHeader(HTTP.CONTENT_TYPE, "application/json; charset=" + SystemConstant.CHARSET_NAME);
|
|
|
|
+ post.setHeader("Accept", "application/json");
|
|
|
|
+
|
|
|
|
+ String encoderJson = URLEncoder.encode(json, SystemConstant.CHARSET_NAME);
|
|
|
|
+ StringEntity se = new StringEntity(encoderJson);
|
|
|
|
+ se.setContentType("text/json");
|
|
|
|
+ se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
|
|
|
|
+ post.setEntity(se);
|
|
|
|
+ // 执行请求,获取响应
|
|
|
|
+ return getRespString(post);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * post 请求
|
|
|
|
+ *
|
|
|
|
+ * @param url
|
|
|
|
+ * @param params
|
|
|
|
+ * @param secret
|
|
|
|
+ * @param timestamp
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static String post(String url, Map<String, Object> params, String secret, Long timestamp)
|
|
|
|
+ throws IOException {
|
|
|
|
+ // 构建post请求
|
|
|
|
+ HttpPost post = new HttpPost(url);
|
|
|
|
+ post.setHeader(SystemConstant.HEADER_AUTHORIZATION, secret);
|
|
|
|
+ post.setHeader(SystemConstant.HEADER_TIME, String.valueOf(timestamp));
|
|
|
|
+ post.setHeader(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded;charset=" + SystemConstant.CHARSET_NAME);
|
|
|
|
+ // 构建请求参数
|
|
|
|
+ List<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
|
|
|
|
+ if (params != null) {
|
|
|
|
+ for (String key : params.keySet()) {
|
|
|
|
+ pairs.add(new BasicNameValuePair(key, String.valueOf(params.get(key))));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ HttpEntity entity = null;
|
|
|
|
+ try {
|
|
|
|
+ entity = new UrlEncodedFormEntity(pairs, SystemConstant.CHARSET_NAME);
|
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
|
+ log.error(SystemConstant.LOG_ERROR, e);
|
|
|
|
+ }
|
|
|
|
+ post.setEntity(entity);
|
|
|
|
+ // 执行请求,获取响应
|
|
|
|
+ return getRespString(post);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * post 请求
|
|
|
|
+ *
|
|
|
|
+ * @param url
|
|
|
|
+ * @param params
|
|
|
|
+ * @param accessToken
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static String post(String url, Map<String, Object> params, String accessToken) throws IOException {
|
|
|
|
+ // 构建post请求
|
|
|
|
+ HttpPost post = new HttpPost(url);
|
|
|
|
+ post.setHeader(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded;charset=" + SystemConstant.CHARSET_NAME);
|
|
|
|
+ post.setHeader("X-Access-Token", accessToken);
|
|
|
|
+ // 构建请求参数
|
|
|
|
+ List<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
|
|
|
|
+ if (params != null) {
|
|
|
|
+ for (String key : params.keySet()) {
|
|
|
|
+ pairs.add(new BasicNameValuePair(key, String.valueOf(params.get(key))));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ HttpEntity entity = null;
|
|
|
|
+ try {
|
|
|
|
+ entity = new UrlEncodedFormEntity(pairs, SystemConstant.CHARSET_NAME);
|
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
|
+ log.error(SystemConstant.LOG_ERROR, e);
|
|
|
|
+ }
|
|
|
|
+ post.setEntity(entity);
|
|
|
|
+ // 执行请求,获取响应
|
|
|
|
+ return getRespString(post);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * get 请求
|
|
|
|
+ *
|
|
|
|
+ * @param url
|
|
|
|
+ * @param params
|
|
|
|
+ * @param secret
|
|
|
|
+ * @param timestamp
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static String get(String url, Map<String, Object> params, String secret, Long timestamp) throws IOException {
|
|
|
|
+ // 构建请求参数
|
|
|
|
+ List<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
|
|
|
|
+ if (params != null) {
|
|
|
|
+ for (String key : params.keySet()) {
|
|
|
|
+ pairs.add(new BasicNameValuePair(key, String.valueOf(params.get(key))));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ String str = EntityUtils.toString(new UrlEncodedFormEntity(pairs, StandardCharsets.UTF_8));// 转换为键值对
|
|
|
|
+ HttpGet get = new HttpGet(url + SystemConstant.GET_UNKNOWN + str);
|
|
|
|
+ if (Objects.nonNull(secret)) {
|
|
|
|
+ get.setHeader(SystemConstant.HEADER_AUTHORIZATION, secret);
|
|
|
|
+ }
|
|
|
|
+ if (Objects.nonNull(timestamp)) {
|
|
|
|
+ get.setHeader(SystemConstant.HEADER_TIME, String.valueOf(timestamp));
|
|
|
|
+ }
|
|
|
|
+ // 执行请求,获取响应
|
|
|
|
+ return getRespString(get);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取响应信息
|
|
|
|
+ *
|
|
|
|
+ * @param request
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static String getRespString(HttpUriRequest request) throws IOException {
|
|
|
|
+ // 获取响应流
|
|
|
|
+ InputStream in = null;
|
|
|
|
+ ByteArrayOutputStream ou = null;
|
|
|
|
+ try {
|
|
|
|
+ in = getRespInputStream(request);
|
|
|
|
+ ou = new ByteArrayOutputStream();
|
|
|
|
+ IOUtils.copy(in, ou);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error(SystemConstant.LOG_ERROR, e);
|
|
|
|
+ } finally {
|
|
|
|
+ if (Objects.nonNull(in)) {
|
|
|
|
+ in.close();
|
|
|
|
+ }
|
|
|
|
+ if (Objects.nonNull(ou)) {
|
|
|
|
+ ou.close();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return Objects.nonNull(ou) ? new String(ou.toByteArray(), StandardCharsets.UTF_8) : null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取输入流
|
|
|
|
+ *
|
|
|
|
+ * @param request
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static InputStream getRespInputStream(HttpUriRequest request) {
|
|
|
|
+ // 获取响应对象
|
|
|
|
+ HttpResponse response = null;
|
|
|
|
+ try {
|
|
|
|
+ response = HttpClients.createDefault().execute(request);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error(SystemConstant.LOG_ERROR, e);
|
|
|
|
+ }
|
|
|
|
+ if (response == null) {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ // 获取Entity对象
|
|
|
|
+ HttpEntity entity = response.getEntity();
|
|
|
|
+ // 获取响应信息流
|
|
|
|
+ InputStream in = null;
|
|
|
|
+ if (entity != null) {
|
|
|
|
+ try {
|
|
|
|
+ in = entity.getContent();
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error(SystemConstant.LOG_ERROR, e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return in;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * post 广药请求
|
|
|
|
+ *
|
|
|
|
+ * @param url
|
|
|
|
+ * @param params
|
|
|
|
+ * @param appid
|
|
|
|
+ * @param secret
|
|
|
|
+ * @param timestamp
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static String postGdpu(String url, Map<String, Object> params, String appid, String secret, Long timestamp)
|
|
|
|
+ throws IOException {
|
|
|
|
+ // 构建post请求
|
|
|
|
+ HttpPost post = new HttpPost(url);
|
|
|
|
+ post.setHeader(SystemConstant.GDPU_HEADER_APP_ID, appid);
|
|
|
|
+ post.setHeader(SystemConstant.GDPU_HEADER_APP_SECRET, secret);
|
|
|
|
+ post.setHeader(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded;charset=" + SystemConstant.CHARSET_NAME);
|
|
|
|
+ // 构建请求参数
|
|
|
|
+ List<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
|
|
|
|
+ if (params != null) {
|
|
|
|
+ for (String key : params.keySet()) {
|
|
|
|
+ pairs.add(new BasicNameValuePair(key, String.valueOf(params.get(key))));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ HttpEntity entity = null;
|
|
|
|
+ try {
|
|
|
|
+ entity = new UrlEncodedFormEntity(pairs, SystemConstant.CHARSET_NAME);
|
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
|
+ log.error(SystemConstant.LOG_ERROR, e);
|
|
|
|
+ }
|
|
|
|
+ post.setEntity(entity);
|
|
|
|
+ // 执行请求,获取响应
|
|
|
|
+ return getRespString(post);
|
|
|
|
+ }
|
|
|
|
+}
|