From 6741baac0cdaa19e0ab9fa8ccd595337abb6dbaa Mon Sep 17 00:00:00 2001 From: 101captain <237651143@qq.com> Date: 星期一, 30 八月 2021 14:18:30 +0800 Subject: [PATCH] 微心愿改版 --- springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/HttpClientUtil.java | 199 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 193 insertions(+), 6 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 a2ade11..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 @@ -7,19 +7,23 @@ import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; 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; +import java.util.Iterator; +import java.util.List; +import java.util.Map; /** * @program: springcloud_k8s_panzhihuazhihuishequ @@ -119,8 +123,7 @@ String line = ""; StringBuffer sb = new StringBuffer(); for (line = br.readLine(); line != null; line = br.readLine()) { - sb.append(br.readLine()); - System.out.println(line); + sb.append(line); } return sb.toString(); } catch (MalformedURLException e) { @@ -132,4 +135,188 @@ } + /** + * http请求工具类,get请求 + * + * @param url + * @param params + * @param resonseCharSet + * @return + * @throws Exception + */ + public static String httpGet(String url, Map<String, Object> params, String... resonseCharSet) throws Exception { + DefaultHttpClient defaultHttpClient = null; + BufferedReader bufferedReader = null; + try { + defaultHttpClient = new DefaultHttpClient(); + if (params != null) { + StringBuilder stringBuilder = new StringBuilder(); + Iterator<String> iterator = params.keySet().iterator(); + String key; + while (iterator.hasNext()) { + key = iterator.next(); + Object val = params.get(key); + if (val instanceof List) { + List v = (List) val; + for (Object o : v) { + stringBuilder.append(key).append("=").append(o.toString()).append("&"); + } + } else { + stringBuilder.append(key).append("=").append(val.toString()).append("&"); + } + } + stringBuilder.deleteCharAt(stringBuilder.length() - 1); + url = url + "?" + stringBuilder.toString(); + log.info("url:{}", url); + } + HttpGet httpGet = new HttpGet(url); + httpGet.setHeader("Content-Type", "application/json;charset=ut-8"); + HttpResponse httpResponse = defaultHttpClient.execute(httpGet); + if (httpResponse.getStatusLine().getStatusCode() != 200) { + String errorLog = "请求失败,errorCode:" + httpResponse.getStatusLine().getStatusCode(); + log.info(errorLog); + throw new Exception(url + errorLog); + } + //读取返回信息 + String charSet = "utf-8"; + if (resonseCharSet != null && resonseCharSet.length > 0) + charSet = resonseCharSet[0]; + String output; + bufferedReader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), charSet)); + + StringBuilder dataBuilder = new StringBuilder(); + while ((output = bufferedReader.readLine()) != null) { + dataBuilder.append(output); + } + return dataBuilder.toString(); + } catch (IOException e) { + e.printStackTrace(); + throw e; + } finally { + if (defaultHttpClient != null) + defaultHttpClient.getConnectionManager().shutdown(); + if (bufferedReader != null) + bufferedReader.close(); + } + } + + /** + * http请求工具类,post请求 + * + * @param url url + * @param param 参数值 仅支持String + * @return + * @throws Exception + */ + public static String httpPost(String url, String param) throws Exception { + DefaultHttpClient defaultHttpClient = null; + BufferedReader bufferedReader = null; + try { + defaultHttpClient = new DefaultHttpClient(); + HttpPost httpPost = new HttpPost(url); + httpPost.setHeader("Content-Type", "application/json;charset=ut-8"); + if (StringUtils.isNotBlank(param)) { + log.info("参数值:{}", param); + HttpEntity httpEntity = new StringEntity(param, "utf-8"); + httpPost.setEntity(httpEntity); + } + HttpResponse httpResponse = defaultHttpClient.execute(httpPost); + if (httpResponse.getStatusLine().getStatusCode() != 200) { + String errorLog = "请求失败,errorCode:" + httpResponse.getStatusLine().getStatusCode(); + log.info(errorLog); + throw new Exception(url + errorLog); + } + //读取返回信息 + String output; + bufferedReader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), "utf-8")); + StringBuilder stringBuilder = new StringBuilder(); + while ((output = bufferedReader.readLine()) != null) { + stringBuilder.append(output); + } + log.info("调用微信接口返回的参数:" + stringBuilder.toString()); + return stringBuilder.toString(); + } catch (IOException e) { + e.printStackTrace(); + throw e; + } finally { + if (defaultHttpClient != null) + defaultHttpClient.getConnectionManager().shutdown(); + if (bufferedReader != null) + bufferedReader.close(); + } + } + + /** + * 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