微信与第三方服务器之间的通信大致流程

来源:互联网 发布:ubuntu卡死 编辑:程序博客网 时间:2024/05/13 20:27

最近忙着做微信,https协议不会,只好先用http实现业务逻辑,对于微信与第三方服务器之间的通信流程有个大致的了解,备忘。

首先,微信开发文档中多次提到的XML消息体加解密,其中需要的参数诸如timestamp,nonce,msg_signature都是在请求url参数中传过来的。


直观图解及其调试代码(要注意微信后台选的加密模式,兼容好调试):



System.out.println("post...");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
/**
* 加密过的XML消息体
*/
byte[] bytes = new byte[1024 * 1024];
InputStream is = request.getInputStream();

int nRead = 1;
int nTotalRead = 0;
while (nRead > 0) {
nRead = is.read(bytes, nTotalRead, bytes.length - nTotalRead);
if (nRead > 0)
nTotalRead = nTotalRead + nRead;
}
String encryptXML = new String(bytes, 0, nTotalRead, "utf-8");
System.out.println("加密消息体  =  " + encryptXML);

/**
 * 请求url中的参数
 */

Map<String,String> map = new HashMap();
Enumeration paramNames = request.getParameterNames();


while (paramNames.hasMoreElements()) {
String paramName = (String) paramNames.nextElement();
String[] paramValues = request.getParameterValues(paramName);
if (paramValues.length == 1) {
String paramValue = paramValues[0];
if (paramValue.length() != 0) {
System.out.println("参数:" + paramName + "=" + paramValue);
map.put(paramName, paramValue);
}
}
}

System.out.println("post over !");


至此应该对于微信加密的流程以及参数传递有个了解。

PS:异常java.security.InvalidKeyException:illegal Key Size的解决方案

微信开发文档提供的加解密文档示例中readme.txt有解决方法,建议先解决这个再开发



1 0