微信 接入机器人 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;//创建日期

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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • linux 下tar打包举例,Linux tar打包命令

    linux 下tar打包举例,Linux tar打包命令Linuxtar打包命令:范例一:将整个/etc目录下的文件全部打包成为/tmp/etc.tar[root@linux~]#tar-cvf/tmp/etc.tar/etc<==仅打包,不压缩![root@linux~]#tar-zcvf/tmp/etc.tar.gz/etc<==打包后,以gzip压缩[root@linux~]#tar-j…

  • read函数的返回值问题

    read函数的返回值问题Read函数读取字符串返回值的问题1.前言在学习socket编程的途中,通过客户端给服务端发送字符串,然后服务端通过read或者recv来读取数据,然后返回读取的字节数.我在想read返回的读取字节数有没有包含’\0’或者’\n’呢,于是通过一些简单的小例子,来看看实际情况到底如何.2.read函数我们来看一下read函数的原型:ssize_tread(intfd,void…

  • linux查看防火墙状态_Linux关闭防火墙

    linux查看防火墙状态_Linux关闭防火墙service iptablesstatus;systemctlstatus firewalld;关闭防火墙命令:systemctl stop firewalld.service开启防火墙:systemctl startfirewalld.service关闭开机自启动:systemctl disable firewalld.service开启开机启动:syste…

  • python转置矩阵函数_对python 矩阵转置transpose的实例讲解

    python转置矩阵函数_对python 矩阵转置transpose的实例讲解在读图片时,会用到这么的一段代码:image_vector_len=np.prod(image_size)#总元素大小,3*55*47img=Image.open(path)arr_img=np.asarray(img,dtype=’float64′)arr_img=arr_img.transpose(2,0,1).reshape((image_vector_len,))#4…

  • MySQL Longtext字段优化记录(一)[通俗易懂]

    MySQL Longtext字段优化记录(一)[通俗易懂]Longtext优化记录

  • RelativeLayout中子view设置Margin无效[通俗易懂]

    RelativeLayout中子view设置Margin无效[通俗易懂]发现:在RelativeLayout中如果一个子view设置如:layout_alignParentBottom或者layout_alignParentTop等属性,在代码中通过layoutParam动态设置margin会是无效。记录下。修改margin同时改变图片大小:RelativeLayout.MarginLayoutParamslayoutParams=(RelativeLayout

发表回复

您的电子邮箱地址不会被公开。

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