From 7ae1acd794d359908dcf61f22b60dcc3cf0a5c15 Mon Sep 17 00:00:00 2001 From: 无关风月 <443237572@qq.com> Date: 星期三, 19 六月 2024 16:54:29 +0800 Subject: [PATCH] 数据上传修改 --- DriverZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/HttpResult.java | 45 ++++ UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/HttpClientUtil.java | 296 +++++++++++++------------- UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/PushMinistryOfTransportUtil.java | 77 ++++++ UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/ALiApiUtil.java | 7 DriverZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/PushMinistryOfTransportUtil.java | 153 ++++++++++++- UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/HttpResult.java | 45 ++++ UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/PayMoneyUtil.java | 11 7 files changed, 450 insertions(+), 184 deletions(-) diff --git a/DriverZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/HttpResult.java b/DriverZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/HttpResult.java new file mode 100644 index 0000000..fe777d3 --- /dev/null +++ b/DriverZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/HttpResult.java @@ -0,0 +1,45 @@ +package com.stylefeng.guns.modular.system.util; + + +/** + * http请求返回封装 + */ +public class HttpResult { + /** + * 返回状态码 + */ + private Integer code; + /** + * 返回结果 + */ + private String data; + + /** + * 返回封装结果 + * @param code + * @param data + * @return + */ + public static HttpResult getHttpResult(Integer code, String data){ + HttpResult httpResult = new HttpResult(); + httpResult.setCode(code); + httpResult.setData(data); + return httpResult; + } + + public Integer getCode() { + return code; + } + + public void setCode(Integer code) { + this.code = code; + } + + public String getData() { + return data; + } + + public void setData(String data) { + this.data = data; + } +} diff --git a/DriverZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/PushMinistryOfTransportUtil.java b/DriverZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/PushMinistryOfTransportUtil.java index 2e6e41b..3efe0f3 100644 --- a/DriverZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/PushMinistryOfTransportUtil.java +++ b/DriverZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/PushMinistryOfTransportUtil.java @@ -79,8 +79,19 @@ jsonObject.put("UpdateTime", new Date()); Map<String, Object> map = new HashMap<>(); map.put("baseInfoVehicleTotalMile", jsonObject.toJSONString()); - String result = httpClientUtil.pushHttpRequset("POST", path + "ministryOfTransport/baseInfoVehicleTotalMile", map,null,"form"); - System.out.println("网约车车辆里程信息:" + result); + Map<String, String> header = new HashMap<>(); + header.put("Connection", "keep-alive"); + header.put("Content-Type", "application/x-www-form-urlencoded"); + header.put("Accept", "*/*"); + header.put("Accept-Encoding", "gzip"); + header.put("Accept-Charset", "utf-8"); + String result = null; + try { + result = httpClientUtil.pushHttpRequset("POST", path + "ministryOfTransport/baseInfoVehicleTotalMile", map, header,"form").toString(); + } catch (Exception e) { + e.printStackTrace(); + } + System.err.println("--------------------------网约车车辆里程信息------------------------:" + result); } @@ -107,8 +118,19 @@ jsonObject.put("FareType", query.get("id").toString());//运价类型编码 Map<String, Object> map = new HashMap<>(); map.put("orderCreate", jsonObject.toJSONString()); - String result = httpClientUtil.pushHttpRequset("POST", path + "ministryOfTransport/orderCreate", map,null,"form"); - System.out.println("订单发起接口:" + result); + Map<String, String> header = new HashMap<>(); + header.put("Connection", "keep-alive"); + header.put("Content-Type", "application/x-www-form-urlencoded"); + header.put("Accept", "*/*"); + header.put("Accept-Encoding", "gzip"); + header.put("Accept-Charset", "utf-8"); + String result = null; + try { + result = httpClientUtil.pushHttpRequset("POST", path + "ministryOfTransport/orderCreate", map, header,"form").toString(); + } catch (Exception e) { + e.printStackTrace(); + } + System.err.println("---------------------------订单发起接口----------------------:" + result); } @@ -133,8 +155,20 @@ jsonObject.put("DistributeTime", new Date());//派单成功时间YYYYMMDDhhmmss Map<String, Object> map = new HashMap<>(); map.put("orderMatch", jsonObject.toJSONString()); - String result = httpClientUtil.pushHttpRequset("POST", path + "ministryOfTransport/orderMatch", map,null,"form"); - System.out.println("订单成功接口:" + result); + + Map<String, String> header = new HashMap<>(); + header.put("Connection", "keep-alive"); + header.put("Content-Type", "application/x-www-form-urlencoded"); + header.put("Accept", "*/*"); + header.put("Accept-Encoding", "gzip"); + header.put("Accept-Charset", "utf-8"); + String result = null; + try { + result = httpClientUtil.pushHttpRequset("POST", path + "ministryOfTransport/orderMatch", map, header,"form").toString(); + } catch (Exception e) { + e.printStackTrace(); + } + System.err.println("--------------------------------订单成功接口---------------------------:" + result); } @@ -156,8 +190,19 @@ jsonObject.put("Encrypt", 1);//坐标加密标识(1:GCJ-02测绘局标准,2:WGS84 GPS标准,3:BD-09百度标准,4:CGCS2000北斗标准,0:其他) Map<String, Object> map = new HashMap<>(); map.put("operateLogin", jsonObject.toJSONString()); - String result = httpClientUtil.pushHttpRequset("POST", path + "ministryOfTransport/operateLogin", map,null,"form"); - System.out.println("车辆经营上线接口:" + result); + Map<String, String> header = new HashMap<>(); + header.put("Connection", "keep-alive"); + header.put("Content-Type", "application/x-www-form-urlencoded"); + header.put("Accept", "*/*"); + header.put("Accept-Encoding", "gzip"); + header.put("Accept-Charset", "utf-8"); + String result = null; + try { + result = httpClientUtil.pushHttpRequset("POST", path + "ministryOfTransport/operateLogin", map, header,"form").toString(); + } catch (Exception e) { + e.printStackTrace(); + } + System.err.println("--------------------------车辆经营上线接口------------------------:" + result); } } @@ -180,8 +225,19 @@ jsonObject.put("Encrypt", 1);//坐标加密标识(1:GCJ-02测绘局标准,2:WGS84 GPS标准,3:BD-09百度标准,4:CGCS2000北斗标准,0:其他) Map<String, Object> map = new HashMap<>(); map.put("operateLogout", jsonObject.toJSONString()); - String result = httpClientUtil.pushHttpRequset("POST", path + "ministryOfTransport/operateLogout", map,null,"form"); - System.out.println("车辆经营下线接口:" + result); + Map<String, String> header = new HashMap<>(); + header.put("Connection", "keep-alive"); + header.put("Content-Type", "application/x-www-form-urlencoded"); + header.put("Accept", "*/*"); + header.put("Accept-Encoding", "gzip"); + header.put("Accept-Charset", "utf-8"); + String result = null; + try { + result = httpClientUtil.pushHttpRequset("POST", path + "ministryOfTransport/operateLogout", map, header,"form").toString(); + } catch (Exception e) { + e.printStackTrace(); + } + System.err.println("--------------------------------车辆经营下线接口------------------------:" + result); } } @@ -222,8 +278,20 @@ jsonObject.put("WaitTime", orderPrivateCar.getWait() * 60);//等待时间(秒) Map<String, Object> map = new HashMap<>(); map.put("operateDepart", jsonObject.toJSONString()); - String result = httpClientUtil.pushHttpRequset("POST", path + "ministryOfTransport/operateDepart", map,null,"form"); - System.out.println("经营出发接口:" + result); + + Map<String, String> header = new HashMap<>(); + header.put("Connection", "keep-alive"); + header.put("Content-Type", "application/x-www-form-urlencoded"); + header.put("Accept", "*/*"); + header.put("Accept-Encoding", "gzip"); + header.put("Accept-Charset", "utf-8"); + String result = null; + try { + result = httpClientUtil.pushHttpRequset("POST", path + "ministryOfTransport/operateDepart", map,header,"form").toString(); + } catch (Exception e) { + e.printStackTrace(); + } + System.err.println("--------------------------------经营出发接口-------------------------:" + result); } @@ -243,8 +311,19 @@ jsonObject.put("DriveTime", Double.valueOf((orderPrivateCar.getGetoffTime().getTime() - orderPrivateCar.getBoardingTime().getTime()) / 1000).intValue());//载客时间(秒) Map<String, Object> map = new HashMap<>(); map.put("operateArrive", jsonObject.toJSONString()); - String result = httpClientUtil.pushHttpRequset("POST", path + "ministryOfTransport/operateArrive", map,null,"form"); - System.out.println("经营到达接口:" + result); + Map<String, String> header = new HashMap<>(); + header.put("Connection", "keep-alive"); + header.put("Content-Type", "application/x-www-form-urlencoded"); + header.put("Accept", "*/*"); + header.put("Accept-Encoding", "gzip"); + header.put("Accept-Charset", "utf-8"); + String result = null; + try { + result = httpClientUtil.pushHttpRequset("POST", path + "ministryOfTransport/operateArrive", map, header,"form").toString(); + } catch (Exception e) { + e.printStackTrace(); + } + System.err.println("-------------------------------经营到达接口-------------------------:" + result); } @@ -322,8 +401,19 @@ jsonObject.put("InvoiceStatus", "0");//发票状态(0:未开票,1:已开票,2:未知) Map<String, Object> map = new HashMap<>(); map.put("operatePay", jsonObject.toJSONString()); - String result = httpClientUtil.pushHttpRequset("POST", path + "ministryOfTransport/operatePay", map,null,"form"); - System.out.println("经营支付接口:" + result); + Map<String, String> header = new HashMap<>(); + header.put("Connection", "keep-alive"); + header.put("Content-Type", "application/x-www-form-urlencoded"); + header.put("Accept", "*/*"); + header.put("Accept-Encoding", "gzip"); + header.put("Accept-Charset", "utf-8"); + String result = null; + try { + result = httpClientUtil.pushHttpRequset("POST", path + "ministryOfTransport/operatePay", map, header,"form").toString(); + } catch (Exception e) { + e.printStackTrace(); + } + System.err.println("-----------------------经营支付接口-----------------------:" + result); } @@ -381,8 +471,20 @@ jsonObject.put("OrderId", orderNum);//订单编号 Map<String, Object> map = new HashMap<>(); map.put("positionDriver", jsonObject.toJSONString()); - String result = httpClientUtil.pushHttpRequset("POST", path + "ministryOfTransport/positionDriver", map,null,"form"); - System.out.println("驾驶员定位信息:" + result); + + Map<String, String> header = new HashMap<>(); + header.put("Connection", "keep-alive"); + header.put("Content-Type", "application/x-www-form-urlencoded"); + header.put("Accept", "*/*"); + header.put("Accept-Encoding", "gzip"); + header.put("Accept-Charset", "utf-8"); + String result = null; + try { + result = httpClientUtil.pushHttpRequset("POST", path + "ministryOfTransport/positionDriver", map, header,"form").toString(); + } catch (Exception e) { + e.printStackTrace(); + } + System.err.println("-----------------------------驾驶员定位信息------------------------:" + result); } @@ -440,8 +542,19 @@ jsonObject.put("OrderId", orderNum);//订单编号(非营运状态下填"0") Map<String, Object> map = new HashMap<>(); map.put("positionVehicle", jsonObject.toJSONString()); - String result = httpClientUtil.pushHttpRequset("POST", path + "ministryOfTransport/positionVehicle", map,null,"form"); - System.out.println("车辆定位信息:" + result); + Map<String, String> header = new HashMap<>(); + header.put("Connection", "keep-alive"); + header.put("Content-Type", "application/x-www-form-urlencoded"); + header.put("Accept", "*/*"); + header.put("Accept-Encoding", "gzip"); + header.put("Accept-Charset", "utf-8"); + String result = null; + try { + result = httpClientUtil.pushHttpRequset("POST", path + "ministryOfTransport/positionVehicle", map, header,"form").toString(); + } catch (Exception e) { + e.printStackTrace(); + } + System.err.println("----------------------------车辆定位信息---------------------------:" + result); } diff --git a/UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/ALiApiUtil.java b/UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/ALiApiUtil.java index eceb51b..cf385d9 100644 --- a/UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/ALiApiUtil.java +++ b/UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/ALiApiUtil.java @@ -33,7 +33,12 @@ param.put("key", "1"); param.put("mobile", name); param.put("idcard", code); - String get = httpClientUtil.pushHttpRequset("GET", url, param, header, "form"); + String get = null; + try { + get = httpClientUtil.pushHttpRequset("GET", url, param, header, "form").getData(); + } catch (Exception e) { + e.printStackTrace(); + } JSONObject jsonObject = JSON.parseObject(get); if(jsonObject.getIntValue("error_code") == 0){ JSONObject value = jsonObject.getJSONObject("result"); diff --git a/UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/HttpClientUtil.java b/UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/HttpClientUtil.java index bcaea70..d613fd4 100644 --- a/UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/HttpClientUtil.java +++ b/UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/HttpClientUtil.java @@ -1,5 +1,6 @@ package com.stylefeng.guns.modular.system.util; +import com.alibaba.fastjson.JSON; import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.http.NameValuePair; import org.apache.http.client.config.RequestConfig; @@ -8,6 +9,7 @@ import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; +import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; @@ -24,6 +26,7 @@ import java.io.InputStream; import java.nio.charset.Charset; import java.security.KeyStore; +import java.text.SimpleDateFormat; import java.util.*; import java.util.concurrent.TimeUnit; @@ -43,20 +46,29 @@ /** * 创建一个httpClient对象 */ - private void getHttpCline(){ - //1.创建连接池管理器 - PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(60000, - TimeUnit.MILLISECONDS); - connectionManager.setMaxTotal(1000); - connectionManager.setDefaultMaxPerRoute(50); - - //2.创建httpclient对象 - this.httpClient = HttpClients.custom() +// private void getHttpCline(){ +// //1.创建连接池管理器 +// PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(60000, +// TimeUnit.MILLISECONDS); +// connectionManager.setMaxTotal(1000); +// connectionManager.setDefaultMaxPerRoute(50); +// +// //2.创建httpclient对象 +// this.httpClient = HttpClients.custom() +// .setConnectionManager(connectionManager) +// .disableAutomaticRetries() +// .build(); +// } + private PoolingHttpClientConnectionManager connectionManager; + /** + * 创建一个httpClient对象 + */ + private CloseableHttpClient getHttpCline(){ + return HttpClients.custom() .setConnectionManager(connectionManager) .disableAutomaticRetries() .build(); } - private RequestConfig getRequestConfig(){ return RequestConfig.custom() .setConnectTimeout(60000) @@ -71,7 +83,7 @@ * @param url 请求地址 * @param params 请求参数 */ - private void setPostHttpRequset(String url, Map<String, Object> params, Map<String, String> header, String contentType){ + private CloseableHttpResponse setPostHttpRequset(String url, Map<String, Object> params, Map<String, String> header, String contentType) throws Exception{ HttpPost httpPost = new HttpPost(url); httpPost.setConfig(this.getRequestConfig()); if(null != header){ @@ -83,30 +95,20 @@ if(null != params){ Set<String> keys = params.keySet(); for(String key : keys){ - list.add(new BasicNameValuePair(key, params.get(key).toString())); + list.add(new BasicNameValuePair(key, null == params.get(key) ? null : params.get(key).toString())); } } - try { - switch (contentType){ - case "form": - httpPost.setEntity(new UrlEncodedFormEntity(list, "UTF-8")); - break; - case "json": - ObjectMapper objectMapper = new ObjectMapper(); - String s =objectMapper.writeValueAsString(params); - System.err.println(s); - httpPost.setEntity(new StringEntity(s, Charset.forName("UTF-8"))); - break; - } - this.getHttpCline(); - if(null == this.httpClient){ - this.getHttpCline(); - } - httpResponse = this.httpClient.execute(httpPost); - } catch (IOException e) { - e.printStackTrace(); - this.close(); + switch (contentType){ + case "form": + httpPost.setEntity(new UrlEncodedFormEntity(list, "UTF-8")); + break; + case "json": + ObjectMapper objectMapper = new ObjectMapper(); + String s =objectMapper.writeValueAsString(params); + httpPost.setEntity(new StringEntity(s, ContentType.create(ContentType.APPLICATION_JSON.getMimeType(), Charset.forName("UTF-8")))); + break; } + return getHttpCline().execute(httpPost); } @@ -115,7 +117,7 @@ * @param url 请求地址 * @param params 请求参数 */ - private void setGetHttpRequset(String url, Map<String, Object> params, Map<String, String> header){ + private CloseableHttpResponse setGetHttpRequset(String url, Map<String, Object> params, Map<String, String> header) throws Exception{ StringBuffer sb = new StringBuffer(); String p = ""; if(null != params){ @@ -126,23 +128,86 @@ p = "?" + sb.substring(0, sb.length() - 1); } HttpGet httpGet = new HttpGet(url + p); + httpGet.setConfig(getRequestConfig()); if(null != header){ for(String key : header.keySet()){ httpGet.setHeader(key, header.get(key)); } } - this.getHttpCline(); - if(null == this.httpClient){ - this.getHttpCline(); - } - try { - httpResponse = this.httpClient.execute(httpGet); - } catch (IOException e) { - e.printStackTrace(); - this.close(); - } + return getHttpCline().execute(httpGet); } + /** + * 获取get请求实例 + * @param url 请求地址 + * @param params 请求参数 + */ +// private CloseableHttpResponse setGetHttpRequset(String url, Map<String, Object> params, Map<String, String> header) throws Exception{ +// StringBuffer sb = new StringBuffer(); +// String p = ""; +// if(null != params){ +// Set<String> keys = params.keySet(); +// for(String key : keys){ +// sb.append(key + "=" + params.get(key) + "&"); +// } +// p = "?" + sb.substring(0, sb.length() - 1); +// } +// HttpGet httpGet = new HttpGet(url + p); +// httpGet.setConfig(getRequestConfig()); +// if(null != header){ +// for(String key : header.keySet()){ +// httpGet.setHeader(key, header.get(key)); +// } +// } +// return getHttpCline().execute(httpGet); +// } +// /** +// * 发送http请求 +// * @param mothed "GET、POST、PUT、HEAD、DELETE、HEAD、OPTIONS" +// * @param url 请求地址 +// * @param params 请求参数 +// * @param header 请求头 +// * @param contentType 参数请求方式form/json +// * @return +// */ +// public String pushHttpRequset(String mothed, String url, Map<String, Object> params, Map<String, String> header, String contentType){ +// String content = null; +// switch (mothed){ +// case "GET": +// this.setGetHttpRequset(url, params, header); +// break; +// case "POST": +// this.setPostHttpRequset(url, params, header, contentType); +// break; +// } +// if(httpResponse.getStatusLine().getStatusCode() == 200){ +// try { +// content = EntityUtils.toString(httpResponse.getEntity()); +// this.close(); +// return content; +// } catch (IOException e) { +// e.printStackTrace(); +// this.close(); +// } +// } +// if(httpResponse.getStatusLine().getStatusCode() == 201){ +// content = "{\"status\":201}"; +// this.close(); +// return content; +// }else{ +// try { +// System.err.println("返回状态码:" + httpResponse.getStatusLine() + "。"); +// content = EntityUtils.toString(httpResponse.getEntity()); +// this.close(); +// return content; +// } catch (IOException e) { +// e.printStackTrace(); +// this.close(); +// } +// } +// this.close(); +// return content; +// } /** * 发送http请求 @@ -153,46 +218,26 @@ * @param contentType 参数请求方式form/json * @return */ - public String pushHttpRequset(String mothed, String url, Map<String, Object> params, Map<String, String> header, String contentType){ - String content = null; + public HttpResult pushHttpRequset(String mothed, String url, Map<String, Object> params, Map<String, String> header, String contentType) throws Exception{ + String randome = UUID.randomUUID().toString(); + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:S"); + System.err.println(sdf.format(new Date()) + "----(" + randome + ")请求参数:" + JSON.toJSONString(params)); + CloseableHttpResponse httpResponse = null; switch (mothed){ case "GET": - this.setGetHttpRequset(url, params, header); + httpResponse = this.setGetHttpRequset(url, params, header); break; case "POST": - this.setPostHttpRequset(url, params, header, contentType); + httpResponse = setPostHttpRequset(url, params, header, contentType); break; } - if(httpResponse.getStatusLine().getStatusCode() == 200){ - try { - content = EntityUtils.toString(httpResponse.getEntity()); - this.close(); - return content; - } catch (IOException e) { - e.printStackTrace(); - this.close(); - } - } - if(httpResponse.getStatusLine().getStatusCode() == 201){ - content = "{\"status\":201}"; - this.close(); - return content; - }else{ - try { - System.err.println("返回状态码:" + httpResponse.getStatusLine() + "。"); - content = EntityUtils.toString(httpResponse.getEntity()); - this.close(); - return content; - } catch (IOException e) { - e.printStackTrace(); - this.close(); - } - } - this.close(); - return content; + int statusCode = httpResponse.getStatusLine().getStatusCode(); + String content = EntityUtils.toString(httpResponse.getEntity(), "UTF-8"); + System.err.println(sdf.format(new Date()) + "----(" + randome + ")返回结果:" + content); + HttpResult httpResult = HttpResult.getHttpResult(statusCode, content); + this.close(httpResponse); + return httpResult; } - - /** * 发送XML请求 * @param url 请求地址 @@ -200,46 +245,20 @@ * @param header 自定义请求头 * @return */ - public String pushHttpRequsetXml(String url, String xml, Map<String, String> header){ + public HttpResult pushHttpRequsetXml(String url, String xml, Map<String, String> header) throws Exception{ HttpPost httpPost = new HttpPost(url); + httpPost.setConfig(getRequestConfig()); for(String key : header.keySet()){ httpPost.setHeader(key, header.get(key)); } httpPost.setHeader("Content-Type", "application/xml"); - try { - httpPost.setEntity(new StringEntity(xml, "UTF-8")); - this.getHttpCline(); - if(null == this.httpClient){ - this.getHttpCline(); - } - httpResponse = this.httpClient.execute(httpPost); - String content = null; - if(httpResponse.getStatusLine().getStatusCode() == 200){ - try { - content = EntityUtils.toString(httpResponse.getEntity(), "UTF-8"); - this.close(); - return content; - } catch (IOException e) { - e.printStackTrace(); - this.close(); - } - }else{ - try { - content = "返回状态码:" + httpResponse.getStatusLine() + "。" + EntityUtils.toString(httpResponse.getEntity()); - this.close(); - return content; - } catch (IOException e) { - e.printStackTrace(); - this.close(); - } - } - this.close(); - return content; - } catch (IOException e) { - e.printStackTrace(); - this.close(); - } - return null; + httpPost.setEntity(new StringEntity(xml, "UTF-8")); + CloseableHttpResponse httpResponse = getHttpCline().execute(httpPost); + int statusCode = httpResponse.getStatusLine().getStatusCode(); + String content = EntityUtils.toString(httpResponse.getEntity(), "UTF-8"); + HttpResult httpResult = HttpResult.getHttpResult(statusCode, content); + this.close(httpResponse); + return httpResult; } @@ -261,34 +280,18 @@ httpPost.setHeader(key, header.get(key)); } httpPost.setHeader("Content-Type", "application/xml"); - try { - httpPost.setEntity(new StringEntity(xml, "UTF-8")); - this.getHttpCline(); - this.initCert(certPassword, certPath, certType); - httpResponse = this.httpClient.execute(httpPost); - String content = null; - if(httpResponse.getStatusLine().getStatusCode() == 200){ - try { - content = EntityUtils.toString(httpResponse.getEntity(), "UTF-8"); - } catch (IOException e) { - e.printStackTrace(); - this.close(); - } - }else{ - try { - content = "返回状态码:" + httpResponse.getStatusLine() + "。" + EntityUtils.toString(httpResponse.getEntity()); - } catch (IOException e) { - e.printStackTrace(); - this.close(); - } - } - this.close(); - return content; - } catch (IOException e) { - e.printStackTrace(); - this.close(); + httpPost.setEntity(new StringEntity(xml, "UTF-8")); + CloseableHttpClient httpCline = this.initCert(certPassword, certPath, certType); + CloseableHttpResponse httpResponse = httpCline.execute(httpPost); + String content = null; + if(httpResponse.getStatusLine().getStatusCode() == 200){ + content = EntityUtils.toString(httpResponse.getEntity(), "UTF-8"); + }else{ + content = "返回状态码:" + httpResponse.getStatusLine() + "。" + EntityUtils.toString(httpResponse.getEntity()); } - return null; + this.close(httpResponse); + httpCline.close(); + return content; } @@ -299,11 +302,9 @@ * @param certType 证书类型 * @throws Exception */ - private void initCert(String key, String certPath, String certType) throws Exception { + private CloseableHttpClient initCert(String key, String certPath, String certType) throws Exception { KeyStore keyStore = KeyStore.getInstance(certType); -// ClassPathResource cp = new ClassPathResource(certPath); InputStream inputStream = new FileInputStream(new File(certPath)); -// InputStream instream = cp.getInputStream(); try { keyStore.load(inputStream, key.toCharArray()); } finally { @@ -313,28 +314,23 @@ SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] {"TLSv1"}, null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); - this.httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build(); + return HttpClients.custom().setSSLSocketFactory(sslsf).build(); } /** * 关闭资源 */ - private void close(){ + private void close(CloseableHttpResponse httpResponse){ try { - if(null != httpClient){ - httpClient.close(); - } if(null != httpResponse){ + EntityUtils.consume(httpResponse.getEntity());//此处高能,通过源码分析,由EntityUtils是否回收HttpEntity httpResponse.close(); } - } catch (IOException e) { + } catch (Exception e) { e.printStackTrace(); }finally { try { - if(null != httpClient){ - httpClient.close(); - } if(null != httpResponse){ httpResponse.close(); } diff --git a/UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/HttpResult.java b/UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/HttpResult.java new file mode 100644 index 0000000..fe777d3 --- /dev/null +++ b/UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/HttpResult.java @@ -0,0 +1,45 @@ +package com.stylefeng.guns.modular.system.util; + + +/** + * http请求返回封装 + */ +public class HttpResult { + /** + * 返回状态码 + */ + private Integer code; + /** + * 返回结果 + */ + private String data; + + /** + * 返回封装结果 + * @param code + * @param data + * @return + */ + public static HttpResult getHttpResult(Integer code, String data){ + HttpResult httpResult = new HttpResult(); + httpResult.setCode(code); + httpResult.setData(data); + return httpResult; + } + + public Integer getCode() { + return code; + } + + public void setCode(Integer code) { + this.code = code; + } + + public String getData() { + return data; + } + + public void setData(String data) { + this.data = data; + } +} diff --git a/UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/PayMoneyUtil.java b/UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/PayMoneyUtil.java index 6e83217..7daa37a 100644 --- a/UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/PayMoneyUtil.java +++ b/UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/PayMoneyUtil.java @@ -337,7 +337,7 @@ xmlString.append("</xml>"); Map<String, String> map1 = null; - String body1 = httpClientUtil.pushHttpRequsetXml(url, xmlString.toString(), new HashMap<>()); + String body1 = httpClientUtil.pushHttpRequsetXml(url, xmlString.toString(), new HashMap<>()).getData(); //将结果xml解析成map body1 = body1.replaceAll("<!\\[CDATA\\[",""); body1 = body1.replaceAll("]]>", ""); @@ -495,7 +495,12 @@ xmlString.append("</xml>"); Map<String, String> map1 = null; - String body1 = httpClientUtil.pushHttpRequsetXml(url, xmlString.toString(), new HashMap<>()); + String body1 = null; + try { + body1 = httpClientUtil.pushHttpRequsetXml(url, xmlString.toString(), new HashMap<>()).getData(); + } catch (Exception e) { + e.printStackTrace(); + } //将结果xml解析成map body1 = body1.replaceAll("<!\\[CDATA\\[",""); body1 = body1.replaceAll("]]>", ""); @@ -803,7 +808,7 @@ xmlString.append("</xml>"); Map<String, String> map1 = null; - String body1 = httpClientUtil.pushHttpRequsetXml(url, xmlString.toString(), new HashMap<>()); + String body1 = httpClientUtil.pushHttpRequsetXml(url, xmlString.toString(), new HashMap<>()).getData(); //将结果xml解析成map body1 = body1.replaceAll("<!\\[CDATA\\[",""); body1 = body1.replaceAll("]]>", ""); diff --git a/UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/PushMinistryOfTransportUtil.java b/UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/PushMinistryOfTransportUtil.java index 1d6b4ba..58fd40d 100644 --- a/UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/PushMinistryOfTransportUtil.java +++ b/UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/PushMinistryOfTransportUtil.java @@ -85,8 +85,19 @@ jsonObject.put("UpdateTime", new Date()); Map<String, Object> map = new HashMap<>(); map.put("baseInfoPassenger", jsonObject.toJSONString()); - String result = httpClientUtil.pushHttpRequset("POST", path + "ministryOfTransport/baseInfoPassenger", map,null,"form"); - System.out.println("乘客基本信息:" + result); + Map<String, String> header = new HashMap<>(); + header.put("Connection", "keep-alive"); + header.put("Content-Type", "application/x-www-form-urlencoded"); + header.put("Accept", "*/*"); + header.put("Accept-Encoding", "gzip"); + header.put("Accept-Charset", "utf-8"); + String result = null; + try { + result = httpClientUtil.pushHttpRequset("POST", path + "ministryOfTransport/baseInfoPassenger", map, header,"form").getData(); + } catch (Exception e) { + e.printStackTrace(); + } + System.err.println("---------------------------乘客基本信息--------------------------:" + result); } @@ -120,8 +131,19 @@ jsonObject.put("CancelReason", query.getReason());//撤销或违约原因 Map<String, Object> map = new HashMap<>(); map.put("orderCancel", jsonObject.toJSONString()); - String result = httpClientUtil.pushHttpRequset("POST", path + "ministryOfTransport/orderCancel", map,null,"form"); - System.out.println("订单撤销接口:" + result); + Map<String, String> header = new HashMap<>(); + header.put("Connection", "keep-alive"); + header.put("Content-Type", "application/x-www-form-urlencoded"); + header.put("Accept", "*/*"); + header.put("Accept-Encoding", "gzip"); + header.put("Accept-Charset", "utf-8"); + String result = null; + try { + result = httpClientUtil.pushHttpRequset("POST", path + "ministryOfTransport/orderCancel", map, header,"form").getData(); + } catch (Exception e) { + e.printStackTrace(); + } + System.err.println("----------------------------订单撤销接口------------------:" + result); } @@ -199,8 +221,19 @@ jsonObject.put("InvoiceStatus", "0");//发票状态(0:未开票,1:已开票,2:未知) Map<String, Object> map = new HashMap<>(); map.put("operatePay", jsonObject.toJSONString()); - String result = httpClientUtil.pushHttpRequset("POST", path + "ministryOfTransport/operatePay", map,null,"form"); - System.out.println("经营支付接口:" + result); + Map<String, String> header = new HashMap<>(); + header.put("Connection", "keep-alive"); + header.put("Content-Type", "application/x-www-form-urlencoded"); + header.put("Accept", "*/*"); + header.put("Accept-Encoding", "gzip"); + header.put("Accept-Charset", "utf-8"); + String result = null; + try { + result = httpClientUtil.pushHttpRequset("POST", path + "ministryOfTransport/operatePay", map, header,"form").getData(); + } catch (Exception e) { + e.printStackTrace(); + } + System.err.println("------------------------经营支付接口----------------------:" + result); } @@ -219,8 +252,21 @@ jsonObject.put("Detail", orderEvaluate.getContent());//评价内容 Map<String, Object> map = new HashMap<>(); map.put("ratedPassenger", jsonObject.toJSONString()); - String result = httpClientUtil.pushHttpRequset("POST", path + "ministryOfTransport/ratedPassenger", map,null,"form"); - System.out.println("乘客评价信息:" + result); + + Map<String, String> header = new HashMap<>(); + header.put("Connection", "keep-alive"); + header.put("Content-Type", "application/x-www-form-urlencoded"); + header.put("Accept", "*/*"); + header.put("Accept-Encoding", "gzip"); + header.put("Accept-Charset", "utf-8"); + String result = null; + try { + result = httpClientUtil.pushHttpRequset("POST", path + "ministryOfTransport/ratedPassenger", map, header,"form").getData(); + } catch (Exception e) { + e.printStackTrace(); + } + System.err.println("-----------------------------乘客评价信息----------------------:" + result); + } @@ -243,8 +289,19 @@ jsonObject.put("TestDepartment", "广西云森科技有限公司");//服务质量信誉考核机构 Map<String, Object> map = new HashMap<>(); map.put("ratedDriver", jsonObject.toJSONString()); - String result = httpClientUtil.pushHttpRequset("POST", path + "ministryOfTransport/ratedDriver", map,null,"form"); - System.out.println("驾驶员信誉信息:" + result); + Map<String, String> header = new HashMap<>(); + header.put("Connection", "keep-alive"); + header.put("Content-Type", "application/x-www-form-urlencoded"); + header.put("Accept", "*/*"); + header.put("Accept-Encoding", "gzip"); + header.put("Accept-Charset", "utf-8"); + String result = null; + try { + result = httpClientUtil.pushHttpRequset("POST", path + "ministryOfTransport/ratedDriver", map, header,"form").getData(); + } catch (Exception e) { + e.printStackTrace(); + } + System.err.println("---------------------------驾驶员信誉信息-----------------------:" + result); } -- Gitblit v1.7.1