From cdca957e4835e359a89fe4c7c9833ab0c78ee4e3 Mon Sep 17 00:00:00 2001 From: lidongdong <1459917685@qq.com> Date: 星期一, 01 四月 2024 11:18:10 +0800 Subject: [PATCH] 修改西区uu洗车登录接口500 7 --- springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/HttpClientUtil.java | 152 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 144 insertions(+), 8 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 f6021ff..ec449f7 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 @@ -190,7 +190,7 @@ HttpResponse httpResponse = defaultHttpClient.execute(httpGet); if (httpResponse.getStatusLine().getStatusCode() != 200) { String errorLog = "请求失败,errorCode:" + httpResponse.getStatusLine().getStatusCode(); - log.info(errorLog); + log.info("errorLog:{}"+errorLog); throw new Exception(url + errorLog); } // 读取返回信息 @@ -216,6 +216,74 @@ } } + + + /** + * http请求工具类,get请求 + * + * @param url + * @param params + * @param resonseCharSet + * @return + * @throws Exception + */ + public static String httpGet(String url, Map<String, Object> params,Integer code, 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() != code) { + String errorLog = "请求失败,errorCode:" + httpResponse.getStatusLine().getStatusCode(); + log.info("errorLog:{}"+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请求 * @@ -232,7 +300,7 @@ try { defaultHttpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); - httpPost.setHeader("Content-Type", "application/json;charset=ut-8"); + httpPost.setHeader("Content-Type", "application/json;charset=utf-8"); if (StringUtils.isNotBlank(param)) { log.info("参数值:{}", param); HttpEntity httpEntity = new StringEntity(param, "utf-8"); @@ -262,6 +330,78 @@ if (bufferedReader != null) bufferedReader.close(); } + } + + + /** + * http请求工具类,post请求 + * + * @param url + * url + * @param param + * 参数值 仅支持String + * @return + * @throws Exception + */ + public static String httpPostAndToken(String url, String param,Map headerMap) throws Exception { + DefaultHttpClient defaultHttpClient = null; + BufferedReader bufferedReader = null; + try { + defaultHttpClient = new DefaultHttpClient(); + HttpPost httpPost = new HttpPost(url); + httpPost.setHeader("Content-Type", "application/json;charset=utf-8"); +// httpPost.setHeader("Authorization", headerMap.get("Authorization").toString()); +// httpPost.setHeader("Host", headerMap.get("Host").toString()); + + for (Object key : headerMap.keySet()) { + httpPost.setHeader(key.toString(), headerMap.get(key).toString()); + } + if (StringUtils.isNotBlank(param)) { + log.info("参数值:{}", param); + HttpEntity httpEntity = new StringEntity(param, "utf-8"); + httpPost.setEntity(httpEntity); +// httpPost.setHeader("Content-Length", String.valueOf(httpEntity.getContentLength())); + } + HttpResponse httpResponse = defaultHttpClient.execute(httpPost); + if (httpResponse.getStatusLine().getStatusCode() != 200) { + int statusCode = httpResponse.getStatusLine().getStatusCode(); + 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(); + } + } + + public static void main(String[] args) throws IOException { + DefaultHttpClient defaultHttpClient = new DefaultHttpClient(); + HttpPost httpPost = new HttpPost("https://dptest.d-power.com.cn:14404/v1/face"); +// httpPost.setHeader("Content-Type", "application/json;charset=utf-8"); + httpPost.setHeader("Authorization", "DpToken P3JHgjLbyljfLrFnS9OZbATRJmacdt4b"); + httpPost.setHeader("Host", "123.60.2.66"); +// httpPost.setHeader("Content-Length", String.valueOf(entity.getContentLength())); + +// httpPost.setHeader("User-Agent", "Apache-HttpClient/4.5.12 (Java/1.8.0_162)[\\r][\\n]"); + httpPost.setEntity(new StringEntity("{\"positions\":[{\"role\":\"occupant\",\"communityId\":\"64f99b2ed26106d4f0fe93f4\",\"unitId\":\"64f99b2ed26106d4f0fe93f7\"}],\"tel\":\"15696695118\",\"name\":\"四月里3\",\"timeout\":10,\"image\":\"fsdfsdfsdf\"}", "utf-8")); +// httpPost.setHeader("Content-Length", String.valueOf(entity.getContentLength())); + HttpResponse httpResponse = defaultHttpClient.execute(httpPost); + System.out.println(httpResponse.getEntity()); } /** @@ -352,12 +492,8 @@ public static String doRefund(String url, String data,String mchId,Boolean isTest) throws Exception{ KeyStore keyStore = KeyStore.getInstance("PKCS12"); //P12文件目录 证书路径,这里需要你自己修改,linux下还是windows下的根路径 - String filepath = ""; - if(isTest){ - filepath = "D:\\wx\\WXCertUtil\\cert\\"; - }else{ - filepath = "/mnt/data/refund/huacheng/"; - } + String filepath = "/mnt/data/refund/huacheng/"; + System.out.println("filepath->"+filepath); FileInputStream instream = new FileInputStream(filepath+"apiclient_cert.p12"); try { -- Gitblit v1.7.1