微信支付body中文乱码的状况

最后解决的办法就是发送数据时指定编码:out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(),"UTF-8"));java

参考:https://bbs.csdn.net/topics/391022204   22楼git

当时的状况是:算法

项目是微信支付模式一扫码后显示package info not match special pay url微信

在网上查了  说是prepay_id参数的问题,经排查是上一步统一下单没用正确返回prepay_id致使下一步参数不全的状况;测试

因此排查统一下单方法,通过测试发现body中不带中文支付正常,带中文则会形成签名不一致的状况:微信支付

 

以前被误导较多时间的方法(记录一下):编码

1:单独编码body的状况会形成支付页面的产品描述是编码过的字节码,而支付成功后微信返回的产品信息是中文的的状况;url

当时代码是这样编写的:spa

body = MD5Util.MD5Encoding(body);

 

import java.security.MessageDigest; public class MD5Util {private static final char hexDigits1[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; //重要的就是这里,要调这个方法签名才能够
    public static String MD5Encoding(String s) { byte[] btInput = null; try { btInput = s.getBytes("UTF-8"); }catch (Exception e){ } return MD5(btInput, 32); } public static String MD5(String s) { byte[] btInput = s.getBytes(); return MD5(btInput, 32); } public static String MD5_16(String str) { byte[] btInput = str.getBytes(); return MD5(btInput, 16); } private static String MD5(byte[] btInput, int length) { try { // 得到MD5摘要算法的 MessageDigest 对象
            MessageDigest mdInst = MessageDigest.getInstance("MD5"); // MessageDigest mdInst = MessageDigest.getInstance("SHA-1"); // 使用指定的字节更新摘要
 mdInst.update(btInput); // 得到密文
            byte[] md = mdInst.digest(); // 把密文转换成十六进制的字符串形式
            int j = md.length; char str[] = new char[j * 2]; int k = 0; for (byte byte0 : md) { str[k++] = hexDigits1[byte0 >>> 4 & 0xf]; str[k++] = hexDigits1[byte0 & 0xf]; } String result = new String(str); return length == 16 ? result.substring(8, 24) : result; } catch (Exception e) { e.printStackTrace(); return null; } } }

2:想到的方法就是把发送的xml文件设置编码(不稳定);data = new String(data.getBytes(), "utf-8");.net

相关文章
相关标签/搜索