问题描述(question)

在微信公众号支付的回调中, 通过以下代码取不到return_code参数

private static void doNotify(HttpServletRequest req, HttpServletResponse resp) throws IOException {
	System.out.println("进入回调.................................");
	resp.setContentType("text/xml;charset=utf-8");
	resp.setCharacterEncoding("utf-8");
	
	String resultCode = req.getParameter("return_code");
	System.out.println("进入回调."+resultCode);
	//这里得到 resultCode  未 null
	
	//TODO 其它代码...
}				

问题原因

微信公众号支付的回调中,返回的参数并不在url中,而是在请求的body中.

解决方案

参考代码:

private static void doNotify(HttpServletRequest req, HttpServletResponse resp) throws IOException {
	System.out.println("进入回调.................................");
	resp.setContentType("text/xml;charset=utf-8");
	resp.setCharacterEncoding("utf-8");

	try {
		InputStream inputStream = req.getInputStream();	
		SAXReader reader = new SAXReader();
		Document doc = reader.read(in);
		System.out.println(doc.asXML());
		//TODO 在doc中有相应的参数,例如result_code
	} catch (DocumentException e) {
		e.printStackTrace();
	}

	//TODO 其它代码...
}