From 84650c7992cdb31d01a926869a3340ff172dccd3 Mon Sep 17 00:00:00 2001
From: tangxiaobao <303826152@qq.com>
Date: 星期五, 13 八月 2021 15:43:14 +0800
Subject: [PATCH] 实有人口导入bug修改、优化
---
springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/HttpClientUtil.java | 78 +++++++++++++++++++++++++++++++++++++--
1 files changed, 74 insertions(+), 4 deletions(-)
diff --git a/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/HttpClientUtil.java b/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/HttpClientUtil.java
index 5bf6c6f..ea451de 100644
--- a/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/HttpClientUtil.java
+++ b/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/HttpClientUtil.java
@@ -9,16 +9,15 @@
import org.apache.http.client.HttpClient;
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.DefaultHttpClient;
+import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import org.springframework.util.ObjectUtils;
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.io.OutputStreamWriter;
+import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
@@ -234,6 +233,7 @@
while ((output = bufferedReader.readLine()) != null) {
stringBuilder.append(output);
}
+ log.info("调用微信接口返回的参数:" + stringBuilder.toString());
return stringBuilder.toString();
} catch (IOException e) {
e.printStackTrace();
@@ -246,7 +246,77 @@
}
}
+ /**
+ * get请求
+ * @param url 请求地址(get请求时参数自己组装到url上)
+ * @param headerMap 请求头
+ * @return 响应文本
+ */
+ public static String get(String url, Map<String, String> headerMap,String param) {
+ // 请求地址,以及参数设置
+ HttpPost post = new HttpPost(url);
+ if (headerMap != null) {
+ for (Map.Entry<String, String> entry : headerMap.entrySet()) {
+ post.setHeader(entry.getKey(), entry.getValue());
+ }
+ }
+ if (StringUtils.isNotBlank(param)) {
+ log.info("参数值:{}", param);
+ HttpEntity httpEntity = new StringEntity(param, "utf-8");
+ post.setEntity(httpEntity);
+ }
+ // 执行请求,获取相应
+ return getRespString(post);
+ }
+ /**
+ * 获取响应信息(String)
+ */
+ public static String getRespString(HttpUriRequest request) {
+ // 获取响应流
+ InputStream in = getRespInputStream(request);
+ StringBuilder sb = new StringBuilder();
+ String line;
+
+ BufferedReader br = new BufferedReader(new InputStreamReader(in));
+ try {
+ while ((line = br.readLine()) != null) {
+ sb.append(line);
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ String str = sb.toString();
+ return str;
+ }
+
+ /**
+ * 获取响应信息(InputStream)
+ */
+ public static InputStream getRespInputStream(HttpUriRequest request) {
+ // 获取响应对象
+ HttpResponse response = null;
+ try {
+ response = HttpClients.createDefault().execute(request);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ if (response == null) {
+ return null;
+ }
+ // 获取Entity对象
+ HttpEntity entity = response.getEntity();
+ // 获取响应信息流
+ InputStream in = null;
+ if (entity != null) {
+ try {
+ in = entity.getContent();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ return in;
+ }
}
--
Gitblit v1.7.1