微信支付body中带有中文字符乱码的问题

今天午饭吃了过后就一直在解决这个问题,网上各种百度,各种博客看了一个遍,但是就是找不到问题,什么服务器编码格式改了,代码中有事各种转码,还有微信支付的key重置,都试了一个遍,但是还是解决不了问题,就连小程序的前段提交方式有post改为get都没有用,最主要是在自己电脑上面完全没有问题,一放到公网服务器就出现问题,总是编码格式有问题,并且返回的xml并没有问题,最后终于发现问题错在了MD5编码

private final static String MD5(String s) {
		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
		try {
			MessageDigest mdInst = MessageDigest.getInstance("MD5");
			mdInst.update(s.getBytes());
			byte[] md = mdInst.digest();
			int j = md.length;
			char str[] = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = md[i];
				str[k++] = hexDigits[byte0 >>> 4 & 0xf];
				str[k++] = hexDigits[byte0 & 0xf];
			}
			return new String(str);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

上面

mdInst.update(s.getBytes());

部分是修改之前的代码,下面

mdInst.update(s.getBytes("UTF-8"));
这是修改之后的代码
private final static String MD5(String s) {
		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
		try {
			MessageDigest mdInst = MessageDigest.getInstance("MD5");
			mdInst.update(s.getBytes("UTF-8"));
			byte[] md = mdInst.digest();
			int j = md.length;
			char str[] = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = md[i];
				str[k++] = hexDigits[byte0 >>> 4 & 0xf];
				str[k++] = hexDigits[byte0 & 0xf];
			}
			return new String(str);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}


就这一个小地方除了这么一个问题,一下午没有找到,弄到晚上十点过才发现,现在问题解决了,可以好好回家过年了……

猜你喜欢

转载自blog.csdn.net/qq_39651858/article/details/79307444