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

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

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

当时的情况是:

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

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

所以排查统一下单方法,经过测试发现body中不带中文支付正常,带中文则会造成签名不一致的情况:

 

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

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

当时代码是这样编写的:

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");

posted @ 2019-04-09 09:02  李晓梦  阅读(1542)  评论(0编辑  收藏  举报