0

0

微信二次开发之文本消息请求与发送

Y2J

Y2J

发布时间:2017-05-10 09:25:10

|

2169人浏览过

|

来源于php中文网

原创

这篇文章主要为大家详细介绍了java微信二次开发第二篇,java微信文本消息接口请求与发送功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

第二篇,做微信文本消息接口请求与发送,具体内容如下

需要导入库:dom4j-1.6.1.jar,xstream-1.3.1.jar

第一步:新建包com.wtz.message.response,新建类BaseMessage.java

package com.wtz.message.response;

/**
 * @author wangtianze QQ:864620012
 * @date 2017年4月19日 下午3:12:40
 * 

version:1.0

*

description:基础消息类

*/ public class BaseMessage { //接收方 private String ToUserName; //发送方 private String FromUserName; //消息的创建时间 private long CreateTime; //消息类型 private String MsgType; public String getToUserName() { return ToUserName; } public void setToUserName(String toUserName) { ToUserName = toUserName; } public String getFromUserName() { return FromUserName; } public void setFromUserName(String fromUserName) { FromUserName = fromUserName; } public long getCreateTime() { return CreateTime; } public void setCreateTime(long createTime) { CreateTime = createTime; } public String getMsgType() { return MsgType; } public void setMsgType(String msgType) { MsgType = msgType; } }

第二步:找到包com.wtz.message.response,新建类TextMessage.java

package com.wtz.message.response;

/**
 *  @author wangtianze QQ:864620012
 * @date 2017年4月19日 下午3:22:33
 * 

version:1.0

*

description:文本消息类

*/ public class TextMessage extends BaseMessage{ //消息内容 private String Content; public String getContent() { return Content; } public void setContent(String content) { Content = content; } }

第三步:找到包com.wtz.util,新建类MessageUtil.java

package com.wtz.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.core.util.QuickWriter;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
import com.thoughtworks.xstream.io.xml.XppDriver;
import com.wtz.message.response.TextMessage;

/**
 *  @author wangtianze QQ:864620012
 * @date 2017年4月19日 下午3:29:58
 * 

version:1.0

*

description:消息处理工具类

Multiavatar
Multiavatar

Multiavatar是一个免费开源的多元文化头像生成器,可以生成高达120亿个虚拟头像

下载
*/ public class MessageUtil { //定义了消息类型(常量:文本类型) public static final String RESP_MESSAGE_TYPE_TEXT = "text"; //从流中解析出每个节点的内容 public static Map parseXml(HttpServletRequest request) throws IOException{ Map map = new HashMap(); //从输入流中获取流对象 InputStream in = request.getInputStream(); //构建SAX阅读器对象 SAXReader reader = new SAXReader(); try { //从流中获得文档对象 Document doc = reader.read(in); //获得根节点 Element root = doc.getRootElement(); //获取根节点下的所有子节点 List children = root.elements(); for(Element e:children){ //遍历每一个节点,并按照节点名--节点值放入map中 map.put(e.getName(), e.getText()); System.out.println("用户发送的消息XML解析为:" + e.getName() + e.getText()); } //关闭流 in.close(); in = null; } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } return map; } /** * 用于扩展节点数据按照,中间加了CDATA段 */ private static XStream xstream = new XStream(new XppDriver(){ public HierarchicalStreamWriter createWriter(Writer out){ return new PrettyPrintWriter(out){ boolean cdata = true; public void startNode(String name,Class clazz){ super.startNode(name,clazz); } protected void writeText(QuickWriter writer,String text){ if(cdata){ writer.write(""); }else{ writer.write(text); } } }; } }); /** * 将文本消息转换成XML格式 */ public static String messageToXml(TextMessage textMessage){ xstream.alias("xml",textMessage.getClass()); String xml = xstream.toXML(textMessage); System.out.println("响应所转换的XML:"+xml); return xml; } }

第四步:找到包com.wtz.service,新建类ProcessService.java

package com.wtz.util;

import java.io.IOException;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.wtz.message.response.TextMessage;

/**
 *  @author wangtianze QQ:864620012
 * @date 2017年4月19日 下午8:04:14
 * 

version:1.0

*

description:核心服务类

*/ public class ProcessService { public static String dealRequest(HttpServletRequest request) throws IOException{ //响应的XML串 String respXml = ""; //要响应的文本内容 String respContent = "未知的消息类型"; Map requestMap = MessageUtil.parseXml(request); String fromUserName = requestMap.get("FromUserName"); String toUserName = requestMap.get("ToUserName"); String MsgType = requestMap.get("MsgType"); String Content = requestMap.get("Content"); System.out.println("用户给公众号发的消息为:" + Content); //构建一条文本消息 TextMessage textMessage = new TextMessage(); textMessage.setToUserName(fromUserName); textMessage.setFromUserName(toUserName); textMessage.setCreateTime(new Date().getTime()); textMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT); if(MsgType.equals(MessageUtil.RESP_MESSAGE_TYPE_TEXT)){ respContent = "王天泽的公众号收到了您的一条文本消息:" + Content + ",时间戳是:" + (new Date().getTime()); } textMessage.setContent(respContent); respXml = MessageUtil.messageToXml(textMessage); System.out.println("respXml:"+respXml); return respXml; } }

第五步:找到包com.wtz.service下的LoginServlet类,重写doPost方法

package com.wtz.service;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wtz.util.MessageUtil;
import com.wtz.util.ProcessService;
import com.wtz.util.ValidationUtil;

/**
 *  @author wangtianze QQ:864620012
 * @date 2017年4月17日 下午8:11:32
 * 

version:1.0

*

description:微信请求验证类

*/ public class LoginServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("get请求。。。。。。"); //1.获得微信签名的加密字符串 String signature = request.getParameter("signature"); //2.获得时间戳信息 String timestamp = request.getParameter("timestamp"); //3.获得随机数 String nonce = request.getParameter("nonce"); //4.获得随机字符串 String echostr = request.getParameter("echostr"); System.out.println("获得微信签名的加密字符串:"+signature); System.out.println("获得时间戳信息:"+timestamp); System.out.println("获得随机数:"+nonce); System.out.println("获得随机字符串:"+echostr); PrintWriter out = response.getWriter(); //验证请求确认成功原样返回echostr参数内容,则接入生效,成为开发者成功,否则失败 if(ValidationUtil.checkSignature(signature, timestamp, nonce)){ out.print(echostr); } out.close(); out = null; } /** * 接受微信服务器发过来的XML数据包(通过post请求发送过来的) */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); //获取微信加密的签名字符串 String signature = request.getParameter("signature"); //获取时间戳 String timestamp = request.getParameter("timestamp"); //获取随机数 String nonce = request.getParameter("nonce"); PrintWriter out = response.getWriter(); if(ValidationUtil.checkSignature(signature,timestamp,nonce)){ String respXml = ""; try { respXml = ProcessService.dealRequest(request); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } out.print(respXml); } out.close(); out = null; } }

完成微信文本消息接口请求与发送。

【相关推荐】

1. 微信公众号平台源码下载

2. 微信投票源码

相关文章

微信app下载
微信app下载

微信是一款手机通信软件,支持通过手机网络发送语音短信、视频、图片和文字。微信可以单聊及群聊,还能根据地理位置找到附近的人,带给大家全新的移动沟通体验,有需要的小伙伴快来保存下载体验吧!

下载

相关标签:

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
go语言 注释编码
go语言 注释编码

本专题整合了go语言注释、注释规范等等内容,阅读专题下面的文章了解更多详细内容。

2

2026.01.31

go语言 math包
go语言 math包

本专题整合了go语言math包相关内容,阅读专题下面的文章了解更多详细内容。

1

2026.01.31

go语言输入函数
go语言输入函数

本专题整合了go语言输入相关教程内容,阅读专题下面的文章了解更多详细内容。

1

2026.01.31

golang 循环遍历
golang 循环遍历

本专题整合了golang循环遍历相关教程,阅读专题下面的文章了解更多详细内容。

0

2026.01.31

Golang人工智能合集
Golang人工智能合集

本专题整合了Golang人工智能相关内容,阅读专题下面的文章了解更多详细内容。

1

2026.01.31

2026赚钱平台入口大全
2026赚钱平台入口大全

2026年最新赚钱平台入口汇总,涵盖任务众包、内容创作、电商运营、技能变现等多类正规渠道,助你轻松开启副业增收之路。阅读专题下面的文章了解更多详细内容。

76

2026.01.31

高干文在线阅读网站大全
高干文在线阅读网站大全

汇集热门1v1高干文免费阅读资源,涵盖都市言情、京味大院、军旅高干等经典题材,情节紧凑、人物鲜明。阅读专题下面的文章了解更多详细内容。

73

2026.01.31

无需付费的漫画app大全
无需付费的漫画app大全

想找真正免费又无套路的漫画App?本合集精选多款永久免费、资源丰富、无广告干扰的优质漫画应用,涵盖国漫、日漫、韩漫及经典老番,满足各类阅读需求。阅读专题下面的文章了解更多详细内容。

67

2026.01.31

漫画免费在线观看地址大全
漫画免费在线观看地址大全

想找免费又资源丰富的漫画网站?本合集精选2025-2026年热门平台,涵盖国漫、日漫、韩漫等多类型作品,支持高清流畅阅读与离线缓存。阅读专题下面的文章了解更多详细内容。

19

2026.01.31

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Kotlin 教程
Kotlin 教程

共23课时 | 3.1万人学习

C# 教程
C# 教程

共94课时 | 8.1万人学习

Java 教程
Java 教程

共578课时 | 54.4万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号