From e51b6e263e1626afe9f7f0f29d7e2c6885a20065 Mon Sep 17 00:00:00 2001 From: xhxu Date: Fri, 1 Apr 2022 16:49:32 +0800 Subject: [PATCH] //add tag --- .../iformall/douyin/pay/DouYinPayHelper.java | 3 +- .../java/com/iformall/utils/HttpUtil.java | 120 +++++++++--------- 2 files changed, 62 insertions(+), 61 deletions(-) diff --git a/mallinkService/src/main/java/com/iformall/douyin/pay/DouYinPayHelper.java b/mallinkService/src/main/java/com/iformall/douyin/pay/DouYinPayHelper.java index 785d8ef8d..779de588d 100644 --- a/mallinkService/src/main/java/com/iformall/douyin/pay/DouYinPayHelper.java +++ b/mallinkService/src/main/java/com/iformall/douyin/pay/DouYinPayHelper.java @@ -120,7 +120,7 @@ public class DouYinPayHelper { public static String doPost(String url, Map params) { - return HttpUtil.payPost(url, JSON.toJSONString(params)); + return HttpUtil.doPost(url,null, JSON.toJSONString(params)); } //服务端预下单 @@ -260,6 +260,7 @@ public class DouYinPayHelper { //小程序开发者为分账方进件 //POST https://developer.toutiao.com/api/apps/ecpay/saas/app_add_sub_merchant public static AppAddSubMerchantResult appAddSubMerchant(AppAddSubMerchant appAddSubMerchant) { +// String response = HttpUtil.doPost("https://developer.toutiao.com/api/apps/ecpay/saas/app_add_sub_merchant",null,JSON.toJSONString(appAddSubMerchant.toRequestMap())); String response = doPost("https://developer.toutiao.com/api/apps/ecpay/saas/app_add_sub_merchant", appAddSubMerchant.toRequestMap()); JSONObject jsonObject = JSON.parseObject(response); Integer code = jsonObject.getInteger("err_no"); diff --git a/mallinkService/src/main/java/com/iformall/utils/HttpUtil.java b/mallinkService/src/main/java/com/iformall/utils/HttpUtil.java index 206d53986..7d78d7706 100644 --- a/mallinkService/src/main/java/com/iformall/utils/HttpUtil.java +++ b/mallinkService/src/main/java/com/iformall/utils/HttpUtil.java @@ -423,64 +423,64 @@ public class HttpUtil { // return null; // } -// /** -// * post请求(用于请求json格式的参数) -// * @param url -// * @param params -// * @return -// */ -// public static String doPost(String url, Map headMap, String params){ -// -// CloseableHttpClient httpclient = HttpClients.createDefault(); -// HttpPost httpPost = new HttpPost(url);// 创建httpPost -// httpPost.setHeader("Accept", "application/json"); -// httpPost.setHeader("Content-Type", "application/json"); -// -// if(headMap != null){ -// Set set = headMap.keySet(); -// for(String key: set){ -// httpPost.addHeader(key,headMap.get(key)); -// } -// } -// -// String charSet = "UTF-8"; -// StringEntity entity = new StringEntity(params, charSet); -// httpPost.setEntity(entity); -// CloseableHttpResponse response = null; -// -// try { -// response = httpclient.execute(httpPost); -// StatusLine status = response.getStatusLine(); -// int state = status.getStatusCode(); -// if (state == HttpStatus.SC_OK) { -// HttpEntity responseEntity = response.getEntity(); -// String jsonString = EntityUtils.toString(responseEntity); -// return jsonString; -// } -// else{ -// logger.info("请求返回:"+state+"("+url+")"); -// } -// } -// catch(Exception e){ -// logger.error(e.getMessage()); -// return null; -// } -// finally { -// if (response != null) { -// try { -// response.close(); -// } catch (IOException e) { -// logger.error(e.getMessage()); -// } -// } -// try { -// httpclient.close(); -// } catch (IOException e) { -// logger.error(e.getMessage()); -// } -// } -// return null; -// } + /** + * post请求(用于请求json格式的参数) + * @param url + * @param params + * @return + */ + public static String doPost(String url, Map headMap, String params){ + + CloseableHttpClient httpclient = HttpClients.createDefault(); + HttpPost httpPost = new HttpPost(url);// 创建httpPost + httpPost.setHeader("Accept", "application/json"); + httpPost.setHeader("Content-Type", "application/json"); + + if(headMap != null){ + Set set = headMap.keySet(); + for(String key: set){ + httpPost.addHeader(key,headMap.get(key)); + } + } + + String charSet = "UTF-8"; + StringEntity entity = new StringEntity(params, charSet); + httpPost.setEntity(entity); + CloseableHttpResponse response = null; + + try { + response = httpclient.execute(httpPost); + StatusLine status = response.getStatusLine(); + int state = status.getStatusCode(); + if (state == HttpStatus.SC_OK) { + HttpEntity responseEntity = response.getEntity(); + String jsonString = EntityUtils.toString(responseEntity); + return jsonString; + } + else{ + logger.info("请求返回:"+state+"("+url+")"); + } + } + catch(Exception e){ + logger.error(e.getMessage()); + return null; + } + finally { + if (response != null) { + try { + response.close(); + } catch (IOException e) { + logger.error(e.getMessage()); + } + } + try { + httpclient.close(); + } catch (IOException e) { + logger.error(e.getMessage()); + } + } + return null; + } public static String payPost(String url, String params) { @@ -504,8 +504,8 @@ public class HttpUtil { private static String exec(okhttp3.Request request) { try { okhttp3.Response response = OkHttpClient.newCall(request).execute(); - String header = response.header("X-Tt-Logid"); - logger.info("X-Tt-Logid="+header); +// String header = response.header("X-Tt-Logid"); +// logger.info("X-Tt-Logid="+header); if (!response.isSuccessful()) throw new RuntimeException("Unexpected code " + response); return response.body().string();