微信支付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");


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM