From 1d16cf86e7cff1f754576620aa27370b91df0004 Mon Sep 17 00:00:00 2001 From: tangxiaobao <303826152@qq.com> Date: 星期五, 23 七月 2021 16:20:46 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/test' into txb --- springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/HttpClientUtil.java | 77 ++++++++++++++++++++++++++++++++++++-- 1 files changed, 73 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 6159238..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; @@ -247,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