微信 接入机器人 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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • vim编辑器

    vim编辑器

    2022年4月3日
    39
  • cf卡,mmc卡,sd卡,sm卡,xd卡,记忆棒的区别是什么?[通俗易懂]

    cf卡,mmc卡,sd卡,sm卡,xd卡,记忆棒的区别是什么?[通俗易懂]乐乐[学长] CF卡(CompactFlash)CF卡是1994年由SanDisk最先推出的。CF卡具有PCMCIA-ATA功能,并与之兼容;CF卡重量只有14g,仅纸板火柴般大小(43mm×36mm×3.3mm),是一种固态产品,也就是工作时没有运动部件。CF卡采用闪存(flash)技术,是一种稳定的存储解决方案,不

    2022年5月27日
    53
  • 二进制加减法计算法则

    二进制加减法计算法则一、二进制加法(逢2进1)举例:100111+11010=10000110011111010——————100001十进制加法是逢十进一,二进制加法是逢二进一。最低位:1加0得1。倒数第2位:1加1得2,同时进1。倒数第3位:1加0得1,再加上进位的1,结果为2。其他位同理。二、二进制减法(借1当2)举例:1000001-11010=100111…

    2022年6月24日
    59
  • DVWA file inclusion 出错 allow_url_fopen=0

    DVWA file inclusion 出错 allow_url_fopen=0在DVWA平台中测试文件包含是出现错误信息include():http://wrapperisdisabledintheserverconfigurationbyallow_url_fopen=0,原因是设置allow_url_open与allow_url_include位置不对,在C:\xampp\htdocs\DVWA\php.ini文件中都设置为on并不会起作用,需要…

    2022年7月16日
    7
  • LeetCode: Valid Palindrome [125]

    LeetCode: Valid Palindrome [125]

    2021年12月16日
    39
  • TK-MyBatis 分页查询「建议收藏」

    TK-MyBatis 分页查询「建议收藏」记tkMybatis查询出一个 List集合该集合已经做好了一层分页Page封装即查询出的list使用类型判断instanceofPage为true但是,中途不明白这是一个带分页的集合,把查询出的结果集又做了一层封装需要返回的对象类型为GoodsCategoryDTO,代码如下:   //商品集合        List<GoodsCategory…

    2022年6月1日
    196

发表回复

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

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