mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2026-02-17 21:56:28 +08:00
🎨 修复重构后的代码
This commit is contained in:
@@ -1,12 +1,9 @@
|
||||
package com.github.binarywang.wxpay.service.impl;
|
||||
|
||||
import java.net.URI;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import javax.net.ssl.SSLContext;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.github.binarywang.wxpay.bean.WxPayApiData;
|
||||
import com.github.binarywang.wxpay.exception.WxPayException;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import jodd.util.Base64;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.http.HttpHost;
|
||||
@@ -28,6 +25,10 @@ import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
import java.net.URI;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 微信支付请求实现类,apache httpclient实现.
|
||||
@@ -37,6 +38,8 @@ import org.apache.http.util.EntityUtils;
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
*/
|
||||
public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
|
||||
private final static JsonParser JSON_PARSER = new JsonParser();
|
||||
|
||||
@Override
|
||||
public byte[] postForBytes(String url, String requestStr, boolean useKey) throws WxPayException {
|
||||
try {
|
||||
@@ -92,18 +95,17 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
|
||||
HttpPost httpPost = this.createHttpPost(url, requestStr);
|
||||
httpPost.addHeader("Accept", "application/json");
|
||||
httpPost.addHeader("Content-Type", "application/json");
|
||||
try (CloseableHttpResponse response = httpClient.execute(httpPost)){
|
||||
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
|
||||
//v3已经改为通过状态码判断200 204 成功
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
String responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
|
||||
if (HttpStatus.SC_OK==statusCode || HttpStatus.SC_NO_CONTENT==statusCode){
|
||||
if (HttpStatus.SC_OK == statusCode || HttpStatus.SC_NO_CONTENT == statusCode) {
|
||||
this.log.info("\n【请求地址】:{}\n【请求数据】:{}\n【响应数据】:{}", url, requestStr, responseString);
|
||||
return responseString;
|
||||
}else {
|
||||
} else {
|
||||
//有错误提示信息返回
|
||||
JSONObject jsonObject = JSONObject.parseObject(responseString);
|
||||
String message = jsonObject.getString("message");
|
||||
throw new WxPayException(message);
|
||||
JsonObject jsonObject = JSON_PARSER.parse(responseString).getAsJsonObject();
|
||||
throw new WxPayException(jsonObject.get("message").getAsString());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
this.log.error("\n【请求地址】:{}\n【请求数据】:{}\n【异常信息】:{}", url, requestStr, e.getMessage());
|
||||
@@ -113,8 +115,6 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -123,18 +123,17 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
|
||||
HttpGet httpGet = new HttpGet(url);
|
||||
httpGet.addHeader("Accept", "application/json");
|
||||
httpGet.addHeader("Content-Type", "application/json");
|
||||
try (CloseableHttpResponse response = httpClient.execute(httpGet)){
|
||||
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
|
||||
//v3已经改为通过状态码判断200 204 成功
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
String responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
|
||||
if (HttpStatus.SC_OK==statusCode || HttpStatus.SC_NO_CONTENT==statusCode){
|
||||
this.log.info("\n【请求地址】:{}\n【响应数据】:{}", url , responseString);
|
||||
if (HttpStatus.SC_OK == statusCode || HttpStatus.SC_NO_CONTENT == statusCode) {
|
||||
this.log.info("\n【请求地址】:{}\n【响应数据】:{}", url, responseString);
|
||||
return responseString;
|
||||
}else {
|
||||
} else {
|
||||
//有错误提示信息返回
|
||||
JSONObject jsonObject = JSONObject.parseObject(responseString);
|
||||
String message = jsonObject.getString("message");
|
||||
throw new WxPayException(message);
|
||||
JsonObject jsonObject = JSON_PARSER.parse(responseString).getAsJsonObject();
|
||||
throw new WxPayException(jsonObject.get("message").getAsString());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
this.log.error("\n【请求地址】:{}\n【异常信息】:{}", url, e.getMessage());
|
||||
@@ -142,25 +141,19 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
|
||||
} finally {
|
||||
httpGet.releaseConnection();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private CloseableHttpClient createApiV3HttpClient() throws WxPayException {
|
||||
CloseableHttpClient apiv3HttpClient = this.getConfig().getApiV3HttpClient();
|
||||
if (null==apiv3HttpClient){
|
||||
if (null == apiv3HttpClient) {
|
||||
return this.getConfig().initApiV3HttpClient();
|
||||
}
|
||||
return apiv3HttpClient;
|
||||
}
|
||||
|
||||
|
||||
private StringEntity createEntry(String requestStr) {
|
||||
|
||||
return new StringEntity(requestStr, ContentType.create("application/json", "utf-8"));
|
||||
//return new StringEntity(new String(requestStr.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
|
||||
|
||||
|
||||
return new StringEntity(requestStr, ContentType.create("application/json", "utf-8"));
|
||||
//return new StringEntity(new String(requestStr.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
|
||||
}
|
||||
|
||||
private HttpClientBuilder createHttpClientBuilder(boolean useKey) throws WxPayException {
|
||||
|
||||
Reference in New Issue
Block a user