微信 接入机器人 java_Java版微信机器人代码

微信 接入机器人 java_Java版微信机器人代码packagecom.eiyoung.wechat.web.controller;importjava.io.IOException;importjava.io.PrintWriter;importjava.util.Date;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;import…

大家好,又见面了,我是你们的朋友全栈君。

package com.eiyoung.wechat.web.controller;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.Date;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import com.eiyoung.wechat.web.utils.Message;

import com.eiyoung.wechat.web.utils.ReplyMessage;

import org.apache.commons.io.IOUtils;

import com.thoughtworks.xstream.XStream;

import com.thoughtworks.xstream.io.xml.DomDriver;

public class WeChat extends HttpServlet {

private static final long serialVersionUID = 1L;

public WeChat() {

super();

* 验证

* @param request

* @param response

* @throws ServletException

* @throws IOException

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

response.setContentType(“text/html;charset=UTF-8”);

PrintWriter pw = response.getWriter();

String echo = request.getParameter(“echostr”);

System.out.print(echo);

echo = new String(echo.getBytes(“ISO-8859-1″),”UTF-8”);

pw.println(echo);

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

response.setContentType(“text/html;charset=UTF-8”);

PrintWriter pw = response.getWriter();

String wxMsgXml = IOUtils.toString(request.getInputStream(),”utf-8″);

Message textMsg = null;

try {

textMsg = getMessage(wxMsgXml);

} catch (Exception e) {

e.printStackTrace();

StringBuffer replyMsg = new StringBuffer();

if(textMsg != null){

//增加你所需要的处理逻辑,这里只是简单重复消息

replyMsg.append(“您给我的消息是:”);

replyMsg.append(textMsg.getContent());

else{

replyMsg.append(“:)不是文本的消息,我暂时看不懂”);

String returnXml = getReplyMessage(replyMsg.toString(), textMsg.getFromUserName(),textMsg.getToUserName());

System.out.print(textMsg.toString());

System.out.print(returnXml.toString());

pw.println(returnXml);

private Message getMessage(String xml){

XStream xstream = new XStream(new DomDriver());

xstream.alias(“xml”, Message.class);

xstream.aliasField(“ToUserName”, Message.class, “toUserName”);

xstream.aliasField(“FromUserName”, Message.class, “fromUserName”);

xstream.aliasField(“CreateTime”, Message.class, “createTime”);

xstream.aliasField(“MsgType”, Message.class, “messageType”);

xstream.aliasField(“Content”, Message.class, “content”);

xstream.aliasField(“MsgId”, Message.class, “msgId”);

Message Message = (Message)xstream.fromXML(xml);

return Message;

private String getReplyMessage(String replyMsg,String toUserName,String fromUserName){

ReplyMessage we = new ReplyMessage();

we.setMessageType(“text”);

we.setFuncFlag(“0”);

we.setCreateTime(new Long(new Date().getTime()).toString());

we.setContent(replyMsg);

we.setToUserName(toUserName);

we.setFromUserName(fromUserName);

XStream xstream = new XStream(new DomDriver());

xstream.alias(“xml”, ReplyMessage.class);

xstream.aliasField(“ToUserName”, ReplyMessage.class, “toUserName”);

xstream.aliasField(“FromUserName”, ReplyMessage.class, “fromUserName”);

xstream.aliasField(“CreateTime”, ReplyMessage.class, “createTime”);

xstream.aliasField(“MsgType”, ReplyMessage.class, “messageType”);

xstream.aliasField(“Content”, ReplyMessage.class, “content”);

xstream.aliasField(“FuncFlag”, ReplyMessage.class, “funcFlag”);

String xml =xstream.toXML(we);

return xml;

package com.eiyoung.wechat.web.utils;

* Created with IntelliJ IDEA.

* User: sb

* Date: 8/1/13

* Time: 10:37 PM

* To change this template use File | Settings | File Templates.

public class ReplyMessage {

private String FuncFlag;//消息编号

private String fromUserName;//发送人

private String toUserName;//接收人

private String content;//内容

private String messageType;//消息类型

private String createTime;//创建日期

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/148838.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长

相关推荐

  • 如何下载离线地图数据(支持谷歌、百度、高德等所有地图源)

    下载离线地图数据(支持谷歌、百度、高德等所有地图源)发布时间:2019-03-12版权:离线地图分为两种:一种叫“金字塔瓦片”数据,一种叫“矢量地图数据”。我们看的在线地图比如,百度,谷歌,高德等等网页上的地图,都是金字塔瓦片;另外一种是手机上用来导航的,比如高德地图APP,也可以下载离线地图数据,这个就是矢量的。卫星影像地图都是金字塔瓦片,无矢量数据。…

    2022年4月5日
  • SPPnet笔记

    SPPnet笔记论文:SpatialPyramidPoolinginDeepConvolutionalNetworksforVisualRecognitionMotivationR-CNN等神经网络都要求输入图片是固定尺寸,比如R-CNN要求输入图片是224×224,这样就限制了其他长宽比和其他尺寸的输入图片。不同长宽比和不同尺寸的输入图片是有意义的,首先,图片数据都不是同一尺寸的,他们大小…

    2022年6月12日
  • 研华acdp手机版_研华acdp值得购买吗

    研华acdp手机版_研华acdp值得购买吗你准备游览一个公园,该公园由 N 个岛屿组成,当地管理部门从每个岛屿出发向另外一个岛屿建了一座桥,不过桥是可以双向行走的。同时,每对岛屿之间都有一艘专用的往来两岛之间的渡船。相对于乘船而言,你更喜欢步行。你希望所经过的桥的总长度尽可能的长,但受到以下的限制:可以自行挑选一个岛开始游览。任何一个岛都不能游览一次以上。无论任何时间你都可以由你现在所在的岛 S 去另一个你从未到过的岛 D。由 S 到 D 可以有以下方法:(1)步行:仅当两个岛之间有一座桥时才有可能。对于这种情况,桥的长度会累加到你步

    2022年8月11日
  • Nginx服务配置编写

    Nginx服务配置编写

    2021年7月2日
  • 缓冲区溢出攻击原理分析

    缓冲区溢出攻击原理分析本文从C/C++语言的函数帧结构出发,分析缓冲溢出攻击如果修改eip进而控制执执行shellcode。

    2022年7月12日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号