mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-08-24 07:23:01 +08:00
🎨 优化部分代码
This commit is contained in:
parent
e19adc802b
commit
31ddb2790c
@ -39,6 +39,10 @@ import java.util.Objects;
|
|||||||
*/
|
*/
|
||||||
public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
|
public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
|
||||||
|
|
||||||
|
private static final String ACCEPT = "Accept";
|
||||||
|
private static final String CONTENT_TYPE = "Content-Type";
|
||||||
|
private static final String APPLICATION_JSON = "application/json";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public byte[] postForBytes(String url, String requestStr, boolean useKey) throws WxPayException {
|
public byte[] postForBytes(String url, String requestStr, boolean useKey) throws WxPayException {
|
||||||
try {
|
try {
|
||||||
@ -92,26 +96,25 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
|
|||||||
public String postV3(String url, String requestStr) throws WxPayException {
|
public String postV3(String url, String requestStr) throws WxPayException {
|
||||||
CloseableHttpClient httpClient = this.createApiV3HttpClient();
|
CloseableHttpClient httpClient = this.createApiV3HttpClient();
|
||||||
HttpPost httpPost = this.createHttpPost(url, requestStr);
|
HttpPost httpPost = this.createHttpPost(url, requestStr);
|
||||||
httpPost.addHeader("Accept", "application/json");
|
httpPost.addHeader(ACCEPT, APPLICATION_JSON);
|
||||||
httpPost.addHeader("Content-Type", "application/json");
|
httpPost.addHeader(CONTENT_TYPE, APPLICATION_JSON);
|
||||||
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
|
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
|
||||||
//v3已经改为通过状态码判断200 204 成功
|
//v3已经改为通过状态码判断200 204 成功
|
||||||
int statusCode = response.getStatusLine().getStatusCode();
|
int statusCode = response.getStatusLine().getStatusCode();
|
||||||
//post方法有可能会没有返回值的情况
|
//post方法有可能会没有返回值的情况
|
||||||
String responseString;
|
String responseString = null;
|
||||||
if (response.getEntity() == null) {
|
if (response.getEntity() != null) {
|
||||||
responseString = null;
|
|
||||||
} else {
|
|
||||||
responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
|
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);
|
this.log.info("\n【请求地址】:{}\n【请求数据】:{}\n【响应数据】:{}", url, requestStr, responseString);
|
||||||
return responseString;
|
return responseString;
|
||||||
} else {
|
|
||||||
//有错误提示信息返回
|
|
||||||
JsonObject jsonObject = GsonParser.parse(responseString);
|
|
||||||
throw convertException(jsonObject);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//有错误提示信息返回
|
||||||
|
JsonObject jsonObject = GsonParser.parse(responseString);
|
||||||
|
throw convertException(jsonObject);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
this.log.error("\n【请求地址】:{}\n【请求数据】:{}\n【异常信息】:{}", url, requestStr, e.getMessage());
|
this.log.error("\n【请求地址】:{}\n【请求数据】:{}\n【异常信息】:{}", url, requestStr, e.getMessage());
|
||||||
throw (e instanceof WxPayException) ? (WxPayException) e : new WxPayException(e.getMessage(), e);
|
throw (e instanceof WxPayException) ? (WxPayException) e : new WxPayException(e.getMessage(), e);
|
||||||
@ -134,26 +137,25 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
|
|||||||
.setSocketTimeout(this.getConfig().getHttpTimeout())
|
.setSocketTimeout(this.getConfig().getHttpTimeout())
|
||||||
.build());
|
.build());
|
||||||
|
|
||||||
httpPatch.addHeader("Accept", "application/json");
|
httpPatch.addHeader(ACCEPT, APPLICATION_JSON);
|
||||||
httpPatch.addHeader("Content-Type", "application/json");
|
httpPatch.addHeader(CONTENT_TYPE, APPLICATION_JSON);
|
||||||
try (CloseableHttpResponse response = httpClient.execute(httpPatch)) {
|
try (CloseableHttpResponse response = httpClient.execute(httpPatch)) {
|
||||||
//v3已经改为通过状态码判断200 204 成功
|
//v3已经改为通过状态码判断200 204 成功
|
||||||
int statusCode = response.getStatusLine().getStatusCode();
|
int statusCode = response.getStatusLine().getStatusCode();
|
||||||
//post方法有可能会没有返回值的情况
|
//post方法有可能会没有返回值的情况
|
||||||
String responseString;
|
String responseString = null;
|
||||||
if (response.getEntity() == null) {
|
if (response.getEntity() != null) {
|
||||||
responseString = null;
|
|
||||||
} else {
|
|
||||||
responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
|
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);
|
this.log.info("\n【请求地址】:{}\n【请求数据】:{}\n【响应数据】:{}", url, requestStr, responseString);
|
||||||
return responseString;
|
return responseString;
|
||||||
} else {
|
|
||||||
//有错误提示信息返回
|
|
||||||
JsonObject jsonObject = GsonParser.parse(responseString);
|
|
||||||
throw convertException(jsonObject);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//有错误提示信息返回
|
||||||
|
JsonObject jsonObject = GsonParser.parse(responseString);
|
||||||
|
throw convertException(jsonObject);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
this.log.error("\n【请求地址】:{}\n【请求数据】:{}\n【异常信息】:{}", url, requestStr, e.getMessage());
|
this.log.error("\n【请求地址】:{}\n【请求数据】:{}\n【异常信息】:{}", url, requestStr, e.getMessage());
|
||||||
throw (e instanceof WxPayException) ? (WxPayException) e : new WxPayException(e.getMessage(), e);
|
throw (e instanceof WxPayException) ? (WxPayException) e : new WxPayException(e.getMessage(), e);
|
||||||
@ -166,8 +168,8 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
|
|||||||
public String postV3WithWechatpaySerial(String url, String requestStr) throws WxPayException {
|
public String postV3WithWechatpaySerial(String url, String requestStr) throws WxPayException {
|
||||||
CloseableHttpClient httpClient = this.createApiV3HttpClient();
|
CloseableHttpClient httpClient = this.createApiV3HttpClient();
|
||||||
HttpPost httpPost = this.createHttpPost(url, requestStr);
|
HttpPost httpPost = this.createHttpPost(url, requestStr);
|
||||||
httpPost.addHeader("Accept", "application/json");
|
httpPost.addHeader(ACCEPT, APPLICATION_JSON);
|
||||||
httpPost.addHeader("Content-Type", "application/json");
|
httpPost.addHeader(CONTENT_TYPE, APPLICATION_JSON);
|
||||||
String serialNumber = getConfig().getVerifier().getValidCertificate().getSerialNumber().toString(16).toUpperCase();
|
String serialNumber = getConfig().getVerifier().getValidCertificate().getSerialNumber().toString(16).toUpperCase();
|
||||||
httpPost.addHeader("Wechatpay-Serial", serialNumber);
|
httpPost.addHeader("Wechatpay-Serial", serialNumber);
|
||||||
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
|
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
|
||||||
@ -182,11 +184,11 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
|
|||||||
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);
|
this.log.info("\n【请求地址】:{}\n【请求数据】:{}\n【响应数据】:{}", url, requestStr, responseString);
|
||||||
return responseString;
|
return responseString;
|
||||||
} else {
|
|
||||||
//有错误提示信息返回
|
|
||||||
JsonObject jsonObject = GsonParser.parse(responseString);
|
|
||||||
throw convertException(jsonObject);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//有错误提示信息返回
|
||||||
|
JsonObject jsonObject = GsonParser.parse(responseString);
|
||||||
|
throw convertException(jsonObject);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
this.log.error("\n【请求地址】:{}\n【请求数据】:{}\n【异常信息】:{}", url, requestStr, e.getMessage());
|
this.log.error("\n【请求地址】:{}\n【请求数据】:{}\n【异常信息】:{}", url, requestStr, e.getMessage());
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@ -214,20 +216,19 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
|
|||||||
//v3已经改为通过状态码判断200 204 成功
|
//v3已经改为通过状态码判断200 204 成功
|
||||||
int statusCode = response.getStatusLine().getStatusCode();
|
int statusCode = response.getStatusLine().getStatusCode();
|
||||||
//post方法有可能会没有返回值的情况
|
//post方法有可能会没有返回值的情况
|
||||||
String responseString;
|
String responseString = null;
|
||||||
if (response.getEntity() == null) {
|
if (response.getEntity() != null) {
|
||||||
responseString = null;
|
|
||||||
} else {
|
|
||||||
responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
|
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【响应数据】:{}", url, responseString);
|
this.log.info("\n【请求地址】:{}\n【响应数据】:{}", url, responseString);
|
||||||
return responseString;
|
return responseString;
|
||||||
} else {
|
|
||||||
//有错误提示信息返回
|
|
||||||
JsonObject jsonObject = GsonParser.parse(responseString);
|
|
||||||
throw convertException(jsonObject);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//有错误提示信息返回
|
||||||
|
JsonObject jsonObject = GsonParser.parse(responseString);
|
||||||
|
throw convertException(jsonObject);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
this.log.error("\n【请求地址】:{}\n【异常信息】:{}", url, e.getMessage());
|
this.log.error("\n【请求地址】:{}\n【异常信息】:{}", url, e.getMessage());
|
||||||
throw (e instanceof WxPayException) ? (WxPayException) e : new WxPayException(e.getMessage(), e);
|
throw (e instanceof WxPayException) ? (WxPayException) e : new WxPayException(e.getMessage(), e);
|
||||||
@ -239,16 +240,16 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
|
|||||||
@Override
|
@Override
|
||||||
public String getV3(String url) throws WxPayException {
|
public String getV3(String url) throws WxPayException {
|
||||||
HttpGet httpGet = new HttpGet(url);
|
HttpGet httpGet = new HttpGet(url);
|
||||||
httpGet.addHeader("Accept", "application/json");
|
httpGet.addHeader(ACCEPT, APPLICATION_JSON);
|
||||||
httpGet.addHeader("Content-Type", "application/json");
|
httpGet.addHeader(CONTENT_TYPE, APPLICATION_JSON);
|
||||||
return this.requestV3(url, httpGet);
|
return this.requestV3(url, httpGet);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getV3WithWechatPaySerial(String url) throws WxPayException {
|
public String getV3WithWechatPaySerial(String url) throws WxPayException {
|
||||||
HttpGet httpGet = new HttpGet(url);
|
HttpGet httpGet = new HttpGet(url);
|
||||||
httpGet.addHeader("Accept", "application/json");
|
httpGet.addHeader(ACCEPT, APPLICATION_JSON);
|
||||||
httpGet.addHeader("Content-Type", "application/json");
|
httpGet.addHeader(CONTENT_TYPE, APPLICATION_JSON);
|
||||||
String serialNumber = getConfig().getVerifier().getValidCertificate().getSerialNumber().toString(16).toUpperCase();
|
String serialNumber = getConfig().getVerifier().getValidCertificate().getSerialNumber().toString(16).toUpperCase();
|
||||||
httpGet.addHeader("Wechatpay-Serial", serialNumber);
|
httpGet.addHeader("Wechatpay-Serial", serialNumber);
|
||||||
return this.requestV3(url, httpGet);
|
return this.requestV3(url, httpGet);
|
||||||
@ -258,24 +259,23 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
|
|||||||
public InputStream downloadV3(String url) throws WxPayException {
|
public InputStream downloadV3(String url) throws WxPayException {
|
||||||
CloseableHttpClient httpClient = this.createApiV3HttpClient();
|
CloseableHttpClient httpClient = this.createApiV3HttpClient();
|
||||||
HttpGet httpGet = new WxPayV3DownloadHttpGet(url);
|
HttpGet httpGet = new WxPayV3DownloadHttpGet(url);
|
||||||
httpGet.addHeader("Accept", ContentType.WILDCARD.getMimeType());
|
httpGet.addHeader(ACCEPT, ContentType.WILDCARD.getMimeType());
|
||||||
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
|
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
|
||||||
//v3已经改为通过状态码判断200 204 成功
|
//v3已经改为通过状态码判断200 204 成功
|
||||||
int statusCode = response.getStatusLine().getStatusCode();
|
int statusCode = response.getStatusLine().getStatusCode();
|
||||||
Header contentType = response.getFirstHeader(HttpHeaders.CONTENT_TYPE);
|
Header contentType = response.getFirstHeader(HttpHeaders.CONTENT_TYPE);
|
||||||
boolean isJsonContentType = Objects.nonNull(contentType) &&
|
boolean isJsonContentType = Objects.nonNull(contentType) && ContentType.APPLICATION_JSON.getMimeType()
|
||||||
ContentType.APPLICATION_JSON.getMimeType().equals(ContentType.parse(String.valueOf(contentType.getValue())).getMimeType());
|
.equals(ContentType.parse(String.valueOf(contentType.getValue())).getMimeType());
|
||||||
if ((HttpStatus.SC_OK == statusCode || HttpStatus.SC_NO_CONTENT == statusCode)
|
if ((HttpStatus.SC_OK == statusCode || HttpStatus.SC_NO_CONTENT == statusCode) && !isJsonContentType) {
|
||||||
&& !isJsonContentType) {
|
|
||||||
this.log.info("\n【请求地址】:{}\n", url);
|
this.log.info("\n【请求地址】:{}\n", url);
|
||||||
return response.getEntity().getContent();
|
return response.getEntity().getContent();
|
||||||
} else {
|
|
||||||
//response里的header有content-type=json说明返回了错误信息
|
|
||||||
//有错误提示信息返回
|
|
||||||
String responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
|
|
||||||
JsonObject jsonObject = GsonParser.parse(responseString);
|
|
||||||
throw convertException(jsonObject);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//response里的header有content-type=json说明返回了错误信息
|
||||||
|
//有错误提示信息返回
|
||||||
|
String responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
|
||||||
|
JsonObject jsonObject = GsonParser.parse(responseString);
|
||||||
|
throw convertException(jsonObject);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
this.log.error("\n【请求地址】:{}\n【异常信息】:{}", url, e.getMessage());
|
this.log.error("\n【请求地址】:{}\n【异常信息】:{}", url, e.getMessage());
|
||||||
throw (e instanceof WxPayException) ? (WxPayException) e : new WxPayException(e.getMessage(), e);
|
throw (e instanceof WxPayException) ? (WxPayException) e : new WxPayException(e.getMessage(), e);
|
||||||
@ -289,16 +289,16 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
|
|||||||
HttpPut httpPut = new HttpPut(url);
|
HttpPut httpPut = new HttpPut(url);
|
||||||
StringEntity entity = this.createEntry(requestStr);
|
StringEntity entity = this.createEntry(requestStr);
|
||||||
httpPut.setEntity(entity);
|
httpPut.setEntity(entity);
|
||||||
httpPut.addHeader("Accept", "application/json");
|
httpPut.addHeader(ACCEPT, APPLICATION_JSON);
|
||||||
httpPut.addHeader("Content-Type", "application/json");
|
httpPut.addHeader(CONTENT_TYPE, APPLICATION_JSON);
|
||||||
return requestV3(url, httpPut);
|
return requestV3(url, httpPut);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String deleteV3(String url) throws WxPayException {
|
public String deleteV3(String url) throws WxPayException {
|
||||||
HttpDelete httpDelete = new HttpDelete(url);
|
HttpDelete httpDelete = new HttpDelete(url);
|
||||||
httpDelete.addHeader("Accept", "application/json");
|
httpDelete.addHeader(ACCEPT, APPLICATION_JSON);
|
||||||
httpDelete.addHeader("Content-Type", "application/json");
|
httpDelete.addHeader(CONTENT_TYPE, APPLICATION_JSON);
|
||||||
return requestV3(url, httpDelete);
|
return requestV3(url, httpDelete);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -311,7 +311,7 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private StringEntity createEntry(String requestStr) {
|
private StringEntity createEntry(String requestStr) {
|
||||||
return new StringEntity(requestStr, ContentType.create("application/json", "utf-8"));
|
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(new String(requestStr.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -328,10 +328,12 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
|
|||||||
|
|
||||||
// 使用代理服务器 需要用户认证的代理服务器
|
// 使用代理服务器 需要用户认证的代理服务器
|
||||||
CredentialsProvider provider = new BasicCredentialsProvider();
|
CredentialsProvider provider = new BasicCredentialsProvider();
|
||||||
provider.setCredentials(new AuthScope(this.getConfig().getHttpProxyHost(), this.getConfig().getHttpProxyPort()),
|
provider.setCredentials(new AuthScope(this.getConfig().getHttpProxyHost(),
|
||||||
new UsernamePasswordCredentials(this.getConfig().getHttpProxyUsername(), this.getConfig().getHttpProxyPassword()));
|
this.getConfig().getHttpProxyPort()),
|
||||||
httpClientBuilder.setDefaultCredentialsProvider(provider);
|
new UsernamePasswordCredentials(this.getConfig().getHttpProxyUsername(),
|
||||||
httpClientBuilder.setProxy(new HttpHost(this.getConfig().getHttpProxyHost(), this.getConfig().getHttpProxyPort()));
|
this.getConfig().getHttpProxyPassword()));
|
||||||
|
httpClientBuilder.setDefaultCredentialsProvider(provider)
|
||||||
|
.setProxy(new HttpHost(this.getConfig().getHttpProxyHost(), this.getConfig().getHttpProxyPort()));
|
||||||
}
|
}
|
||||||
return httpClientBuilder;
|
return httpClientBuilder;
|
||||||
}
|
}
|
||||||
@ -355,9 +357,8 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
|
|||||||
sslContext = this.getConfig().initSSLContext();
|
sslContext = this.getConfig().initSSLContext();
|
||||||
}
|
}
|
||||||
|
|
||||||
SSLConnectionSocketFactory connectionSocketFactory = new SSLConnectionSocketFactory(sslContext,
|
httpClientBuilder.setSSLSocketFactory(new SSLConnectionSocketFactory(sslContext,
|
||||||
new DefaultHostnameVerifier());
|
new DefaultHostnameVerifier()));
|
||||||
httpClientBuilder.setSSLSocketFactory(connectionSocketFactory);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user