使用RestTemplate 对接微信付款码支付接入问题记录
程序员文章站
2022-05-03 23:39:09
对接微信接口时最好不要用 RestTemplate (import org.springframework.web.client.RestTemplate;)spring提供http请求工具类1、RestTemplate 请求微信接口时发生乱码问题(RestTemplate 中默认字符格式是 ios-8859-1) 解决办法修改接受的字符集为utf-8:RestTemplate restTemplate1 = new RestTemplate();List
对接微信接口时最好不要用 RestTemplate (import org.springframework.web.client.RestTemplate;)spring提供http请求工具类
1、 RestTemplate 请求微信接口时发生乱码问题(RestTemplate 中默认字符格式是 ios-8859-1) 解决办法修改接受的字符集为utf-8:
RestTemplate restTemplate1 = new RestTemplate();
List<HttpMessageConverter<?>> converterList = restTemplate1.getMessageConverters();
HttpMessageConverter<?> converterTarget = null;
for (HttpMessageConverter<?> item : converterList) {
if (item.getClass() == StringHttpMessageConverter.class) {
converterTarget = item;
break;
}
}
if (converterTarget != null) {
converterList.remove(converterTarget);
}
HttpMessageConverter<?> converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
converterList.add(converter);
restTemplate1.postForObject("https://api.mch.weixin.qq.com/pay/micropay", "xml参数", String.class);
2、用RestTemplate 去请求对账单(https://api.mch.weixin.qq.com/pay/downloadbill)时 提示错误码:20001 data format error(这里没找到原因,格式传的肯定没问题,至于框架做了啥就不清楚了知道的大佬请评论区留言)
解决办法更换成下面的https 的请求方法,没有使用RestTemplate 了
/**
* 发送https请求
* @param requestUrl 请求地址
* @param requestMethod 请求方式(GET、POST)
* @param outputStr 提交的数据
* @return 返回服务器响应的信息
*/
public String httpsRequest(String requestUrl, String requestMethod, String outputStr) {
try {
// 创建SSLContext对象,并使用我们指定的信任管理器初始化
TrustManager[] tm = { new MyX509TrustManager() };
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, tm, new java.security.SecureRandom());
// 从上述SSLContext对象中得到SSLSocketFactory对象
SSLSocketFactory ssf = sslContext.getSocketFactory();
URL url = new URL(requestUrl);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setSSLSocketFactory(ssf);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
// 设置请求方式(GET/POST)
conn.setRequestMethod(requestMethod);
conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
// 当outputStr不为null时向输出流写数据
if (null != outputStr) {
OutputStream outputStream = conn.getOutputStream();
// 注意编码格式
outputStream.write(outputStr.getBytes("UTF-8"));
outputStream.close();
}
// 从输入流读取返回内容
InputStream inputStream = conn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
StringBuffer buffer = new StringBuffer();
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
// 释放资源
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
inputStream = null;
conn.disconnect();
return buffer.toString();
} catch (ConnectException ce) {
log.error("连接超时:{}", ce);
} catch (Exception e) {
log.error("https请求异常:{}", e);
}
return null;
}
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.X509TrustManager;
/**
* 信任管理器
*
* Title: MyX509TrustManager
*
* Description:
*
* @author
*
* @date 2020年11月18日
*
*/
public class MyX509TrustManager implements X509TrustManager {
// 检查客户端证书
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
// 检查服务器端证书
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
// 返回受信任的X509证书数组
public X509Certificate[] getAcceptedIssuers() {
return null;
}
}
本文地址:https://blog.csdn.net/qq_27292113/article/details/109801507
上一篇: 2020-11-20
下一篇: Java字符串常用的方法汇总