使用微信公众平台自动回复 API 时候,如何向服务器提交 xml 消息内容?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用微信公众平台自动回复 API 时候,如何向服务器提交 xml 消息内容?相关的知识,希望对你有一定的参考价值。

我的开发环境是eclipse+springMvc
"/chat" 是我最终项目的请求Controller URL路径

下面是homecontroller

[java] view plain copy
@Controller
@RequestMapping("/*")
public class HomeController

private String Token = "123456789abcdef";

@RequestMapping(value = "chat", method = RequestMethod.GET, RequestMethod.POST )
@ResponseBody
public void liaotian(Model model, HttpServletRequest request, HttpServletResponse response)
System.out.println("进入chat");
boolean isGet = request.getMethod().toLowerCase().equals("get");
if (isGet)
String signature = request.getParameter("signature");
String timestamp = request.getParameter("timestamp");
String nonce = request.getParameter("nonce");
String echostr = request.getParameter("echostr");
System.out.println(signature);
System.out.println(timestamp);
System.out.println(nonce);
System.out.println(echostr);
access(request, response);
else
// 进入POST聊天处理
System.out.println("enter post");
try
// 接收消息并返回消息
acceptMessage(request, response);
catch (IOException e)
e.printStackTrace();




/**
* 验证URL真实性
*
* @author morning
* @date 2015年2月17日 上午10:53:07
* @param request
* @param response
* @return String
*/
private String access(HttpServletRequest request, HttpServletResponse response)
// 验证URL真实性
System.out.println("进入验证access");
String signature = request.getParameter("signature");// 微信加密签名
String timestamp = request.getParameter("timestamp");// 时间戳
String nonce = request.getParameter("nonce");// 随机数
String echostr = request.getParameter("echostr");// 随机字符串
List<String> params = new ArrayList<String>();
params.add(Token);
params.add(timestamp);
params.add(nonce);
// 1. 将token、timestamp、nonce三个参数进行字典序排序
Collections.sort(params, new Comparator<String>()
@Override
public int compare(String o1, String o2)
return o1.compareTo(o2);

);
// 2. 将三个参数字符串拼接成一个字符串进行sha1加密
String temp = SHA1.encode(params.get(0) + params.get(1) + params.get(2));
if (temp.equals(signature))
try
response.getWriter().write(echostr);
System.out.println("成功返回 echostr:" + echostr);
return echostr;
catch (IOException e)
e.printStackTrace();


System.out.println("失败 认证");
return null;


private void acceptMessage(HttpServletRequest request, HttpServletResponse response) throws IOException
// 处理接收消息
ServletInputStream in = request.getInputStream();
// 将POST流转换为XStream对象
XStream xs = SerializeXmlUtil.createXstream();
xs.processAnnotations(InputMessage.class);
xs.processAnnotations(OutputMessage.class);
// 将指定节点下的xml节点数据映射为对象
xs.alias("xml", InputMessage.class);
// 将流转换为字符串
StringBuilder xmlMsg = new StringBuilder();
byte[] b = new byte[4096];
for (int n; (n = in.read(b)) != -1;)
xmlMsg.append(new String(b, 0, n, "UTF-8"));

// 将xml内容转换为InputMessage对象
InputMessage inputMsg = (InputMessage) xs.fromXML(xmlMsg.toString());

String servername = inputMsg.getToUserName();// 服务端
String custermname = inputMsg.getFromUserName();// 客户端
long createTime = inputMsg.getCreateTime();// 接收时间
Long returnTime = Calendar.getInstance().getTimeInMillis() / 1000;// 返回时间

// 取得消息类型
String msgType = inputMsg.getMsgType();
// 根据消息类型获取对应的消息内容
if (msgType.equals(MsgType.Text.toString()))
// 文本消息
System.out.println("开发者微信号:" + inputMsg.getToUserName());
System.out.println("发送方帐号:" + inputMsg.getFromUserName());
System.out.println("消息创建时间:" + inputMsg.getCreateTime() + new Date(createTime * 1000l));
System.out.println("消息内容:" + inputMsg.getContent());
System.out.println("消息Id:" + inputMsg.getMsgId());

StringBuffer str = new StringBuffer();
str.append("<xml>");
str.append("<ToUserName><![CDATA[" + custermname + "]]></ToUserName>");
str.append("<FromUserName><![CDATA[" + servername + "]]></FromUserName>");
str.append("<CreateTime>" + returnTime + "</CreateTime>");
str.append("<MsgType><![CDATA[" + msgType + "]]></MsgType>");
str.append("<Content><![CDATA[你说的是:" + inputMsg.getContent() + ",吗?]]></Content>");
str.append("</xml>");
System.out.println(str.toString());
response.getWriter().write(str.toString());

// 获取并返回多图片消息
if (msgType.equals(MsgType.Image.toString()))
System.out.println("获取多媒体信息");
System.out.println("多媒体文件id:" + inputMsg.getMediaId());
System.out.println("图片链接:" + inputMsg.getPicUrl());
System.out.println("消息id,64位整型:" + inputMsg.getMsgId());

OutputMessage outputMsg = new OutputMessage();
outputMsg.setFromUserName(servername);
outputMsg.setToUserName(custermname);
outputMsg.setCreateTime(returnTime);
outputMsg.setMsgType(msgType);
ImageMessage images = new ImageMessage();
images.setMediaId(inputMsg.getMediaId());
outputMsg.setImage(images);
System.out.println("xml转换:/n" + xs.toXML(outputMsg));
response.getWriter().write(xs.toXML(outputMsg));




参考技术A 直接echo xml就可以了。 参考技术B msgType.equals(MsgType.Text.toString())

你的这里边的Text怎么获取的

微信公众平台API接口

简介

微信公众平台消息接口为开发者提供了一种新的消息处理方式。微信公众平台消息接口为开发者提供与用户进行消息交互的能力。对于成功接入消息接口的微信公众账号,当用户发消息给公众号,微信公众平台服务器会使用http请求对接入的网址进行消息推送,第三方服务器可通过响应包回复特定结构,从而达到回复消息的目的。

申请消息接口

点击申请,填写网址url和token,其中token可由开发者可以任意填写,用作生成签名。

技术分享

网址接入

公众平台用户提交信息后,微信服务器将发送GET请求到填写的URL上,并且带上四个参数:

技术分享

 

开发者通过检验signature对请求进行校验(下面有校验方式)。若确认此次GET请求来自微信服务器,请原样返回echostr参数内容,则接入生效,否则接入失败。

signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。

加密/校验流程:

将token、timestamp、nonce三个参数进行字典序排序

将三个参数字符串拼接成一个字符串进行sha1加密

开发者获得加密后的字符串可与signature对比,标识该请求来源于微信

 消息推送

当普通微信用户向公众账号发消息时,微信服务器将POST该消息到填写的URL上。结构如下:

文本消息

技术分享

图片消息

技术分享

地理位置消息

技术分享

链接消息

技术分享

事件推送

技术分享

消息回复

对于每一个POST请求,开发者在响应包中返回特定xml结构,对该消息进行响应(现支持回复文本、图文、语音、视频、音乐和对收到的消息进行星标操作)。

微信服务器在五秒内收不到响应会断掉连接。

回复xml结构如下:

回复文本消息

技术分享

回复音乐消息

技术分享

回复图文消息

技术分享

技术分享

注意事项

  1. 用户OpenID对一个公众号是固定唯一的串
  2. 请使用80端口
  3. 用户关注公众号,微信公众平台服务器推送一条内容为Hello2BizUser的文本消息到第三方服务器

 

以上是关于使用微信公众平台自动回复 API 时候,如何向服务器提交 xml 消息内容?的主要内容,如果未能解决你的问题,请参考以下文章

微信公众平台API接口

java微信公众平台开发

微信公众平台向特定用户推送消息

微信公众平台 自动回复消息

给微信公众号发消息,他能收到吗?

微信开发公众号自动回复文字和图文链接