博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
慕课网消息的接收与响应3
阅读量:4325 次
发布时间:2019-06-06

本文共 9623 字,大约阅读时间需要 32 分钟。

 节节都是坑爹的,没有一节是行的

 原来是一行代码写错了导致服务无法运行

 

注意手机端输入的问号要是英文字符的才行,如果是中文字符的问号则在服务器端认不出来

 

不是此微信个人订阅号的创建者关注之后

这个微信个人订阅号被创建者重新关注之后好像也是返回同样的内容。。。

 

 贴一下代码吧:

MessageUtil.java

package com.imooc.util;

import java.io.IOException;

import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.xml.soap.Text;

import org.dom4j.Document;

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

import com.imooc.po.TextMessage;

import com.thoughtworks.xstream.XStream;

public class MessageUtil {

public static final String MESSAGE_TEXT = "text";
public static final String MESSAGE_IMAGE = "image";
public static final String MESSAGE_VOICE = "voice";
public static final String MESSAGE_VIDEO = "video";
public static final String MESSAGE_LINK = "link";
public static final String MESSAGE_LOCATION = "location";
public static final String MESSAGE_EVENT = "event";
public static final String MESSAGE_SUBSCRIBE = "subscribe";
public static final String MESSAGE_UNSUBSCRIBE = "unsubscribe";
public static final String MESSAGE_CLICK = "click";
public static final String MESSAGE_VIEW = "view";
/**
* xml转为map集合
* @param ${param} the ${bare_field_name} to set
* @param ${param} the ${bare_field_name} to set
* @param ${param} the ${bare_field_name} to set
* @param ${param} the ${bare_field_name} to set
*/
public static Map<String,String> xmlToMap(HttpServletRequest request) throws DocumentException, IOException{
Map<String,String> map = new HashMap<String,String>();
SAXReader reader = new SAXReader();
InputStream ins = request.getInputStream();//从request中获取输入流
Document doc = reader.read(ins);
Element root = doc.getRootElement();
List<Element> list = root.elements();
for(Element e:list){
map.put(e.getName(), e.getText());
}
ins.close();
return map;
}
/**
* 将文本消息对象转为xml
* @param ${param} the ${bare_field_name} to set
* @param ${param} the ${bare_field_name} to set
* @param ${param} the ${bare_field_name} to set
* @param ${param} the ${bare_field_name} to set
*/
public static String textMessageToXml(TextMessage textMessage){
XStream xstream = new XStream();
xstream.alias("xml",textMessage.getClass());
return xstream.toXML(textMessage);
}

public static String initText(String toUserName,String fromUserName,String content){

TextMessage text = new TextMessage();
text.setFromUserName(toUserName);
text.setToUserName(fromUserName);
//text.setMsgType(MessageUtil.MESSAGE_EVENT);//这里写错了 导致不能提供服务
text.setMsgType(MessageUtil.MESSAGE_TEXT);
text.setCreateTime(new Date().getTime());
text.setContent(content);
return textMessageToXml(text);
}

/**
*
* 主菜单
*
* @return
*/

public static String menuText(){

StringBuffer sb = new StringBuffer();
sb.append("欢迎您的关注,请按照菜单提示进行操作:\n\n");
sb.append("1、课程介绍");
sb.append("2、慕课网介绍\n\n");
sb.append("回复?调出此菜单。");
return sb.toString();
}

public static String firstMenu(){

StringBuffer sb = new StringBuffer();
sb.append("本套课程介绍微信公众号开发,主要涉及公众号介绍、编辑模式介绍、开发模式介绍等");
return sb.toString();
}

public static String secondMenu(){

StringBuffer sb = new StringBuffer();
sb.append("慕课网是垂直的互联网IT技能免费学习网站。以独家视频教程、在线编程工具、学习计划、问答社区为核心特色。在这里,你可以找到最好的互联网技术牛人,也可以通过免费的在线公开视频课程学习国内领先的互联网IT技术。");
sb.append("慕课网课程涵盖前端开发、PHP、Html5、Android、iOS、Swift等IT前沿技术语言,包括基础课程、实用案例、高级分享三大类型,适合不同阶段的学习人群。以纯干货、短视频的形式为平台特点,为在校学生、职场白领提供了一个迅速提升技能、共同分享进步的学习平台。[1]");
sb.append("4月2日,国内首个IT技能学习类应用——慕课网3.1.0版本在应用宝首发。据了解,在此次上线的版本中,慕课网新增了课程历史记录、相关课程推荐等四大功能,为用户营造更加丰富的移动端IT学习体验");
return sb.toString();
}
}

WX_Interface.java

package net.wxinterface;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.security.MessageDigest;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import javax.servlet.ServletException;

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

import org.dom4j.DocumentException;

import com.imooc.po.TextMessage;

import com.imooc.util.MessageUtil;
public class WX_Interface extends HttpServlet {

/**

* Constructor of the object.
*/
public WX_Interface() {
super();
}

/**

* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

/**

* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//微信加密签名,signature结合了开发者填写的tocken参数和请求中的timestamp参数、nonce参数。
String signature = request.getParameter("signature");
//时间戳
String timestamp = request.getParameter("timestamp");
//随机数
String nonce = request.getParameter("nonce");
String echostr = request.getParameter("echostr");
String tocken = "test";
try{
if(null != signature){
String[] ArrTmp = {tocken,timestamp,nonce};
Arrays.sort(ArrTmp);
StringBuffer sb = new StringBuffer();
for(int i=0;i<ArrTmp.length;i++){
sb.append(ArrTmp[i]);
}
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] bytes = md.digest(new String(sb).getBytes());
StringBuffer buf = new StringBuffer();
for(int i=0;i<bytes.length;i++){
if(((int)bytes[i] & 0xff)<0x10){
buf.append("0");
}
buf.append(Long.toString((int) bytes[i] & 0xff,16));
}
if(signature.equals(buf.toString())){
response.getOutputStream().println(echostr);
}
}
}catch(Exception e){
e.printStackTrace();
}
System.out.println("test0");

System.out.println("doGet");
System.out.println("signature "+signature);
System.out.println("timstamp "+timestamp);
System.out.println("nonce "+nonce);
System.out.println("echostr "+echostr);
System.out.println("doGet");
}

/**

* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
try {
Map<String,String> map = MessageUtil.xmlToMap(request);
String fromUserName = map.get("FromUserName");
String toUserName = map.get("ToUserName");
String msgType = map.get("MsgType");
String content = map.get("Content");
String message = null;
//if("text".equals(msgType)){
if(MessageUtil.MESSAGE_TEXT.equals(msgType)){
if("1".equals(content)){
message = MessageUtil.initText(toUserName,fromUserName,MessageUtil.firstMenu());
}else if("2".equals(content)){
message = MessageUtil.initText(toUserName,fromUserName,MessageUtil.secondMenu());
}else if("?".equals(content)||" ? ".equals(content)){
message = MessageUtil.initText(toUserName,fromUserName,MessageUtil.menuText());
}
/* TextMessage text = new TextMessage();
text.setFromUserName(toUserName);//谁发给你,您就发给谁
text.setToUserName(fromUserName);
text.setMsgType("text");
text.setCreateTime(new Date().getTime());
text.setContent("您发送的消息是:"+content);
message = MessageUtil.textMessageToXml(text);
System.out.println(message);*/
}else if(MessageUtil.MESSAGE_EVENT.equals(msgType)){//添加消息推送的逻辑
String eventType = map.get("Event");
if(MessageUtil.MESSAGE_SUBSCRIBE.equals(eventType)){//消息推送事件的类型
message = MessageUtil.initText(toUserName, fromUserName, content);//关注微信公众号也是微信后台推送这些内容,和
}
}
out .print(message);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
out.close();
}

System.out.println("doPost");

//response.setContentType("text/html");
//PrintWriter out = response.getWriter();
/* StringBuffer strb = new StringBuffer();
ServletInputStream in = request.getInputStream();
BufferedReader breader= new BufferedReader( new InputStreamReader(in,"UTF-8"));
String str = null;
while(null!=(str=breader.readLine())){
strb.append(str);
}
//out.println(str);
System.out.println(strb);*/
//out.flush();
//out.close();
}

/**

* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}

}

TextMessage.java

package com.imooc.po;

public class TextMessage extends BaseMessage{
private String ToUserName;//开发者微信号
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;
}
public String getContent() {
return Content;
}
public void setContent(String content) {
Content = content;
}
public String getMsgId() {
return MsgId;
}
public void setMsgId(String msgId) {
MsgId = msgId;
}
private String FromUserName;//发送方账号(一个OpenID)
private long CreateTime;//消息创建时间(整型)
private String MsgType;//消息类型 text
private String Content;//文本消息内容
private String MsgId;//消息id,64位整型

}

转载于:https://www.cnblogs.com/ZHONGZHENHUA/p/6275019.html

你可能感兴趣的文章
阶段3 3.SpringMVC·_06.异常处理及拦截器_2 SpringMVC异常处理之演示程序异常
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_01.ssm整合说明
查看>>
阶段3 3.SpringMVC·_06.异常处理及拦截器_3 SpringMVC异常处理之异常处理代码编写
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_03ssm整合之编写Spring框架
查看>>
阶段3 3.SpringMVC·_06.异常处理及拦截器_5 SpringMVC拦截器之编写controller
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_05.ssm整合之Spring整合SpringMVC的框架
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_06.ssm整合之编写MyBatis框架
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_07.ssm整合之编写MyBatis框架测试保存的方法
查看>>
阶段3 3.SpringMVC·_06.异常处理及拦截器_7 SpringMVC拦截器之拦截器接口方法演示
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_09.ssm整合之Spring整合MyBatis框架配置事务
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_02.ssm整合之搭建环境
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_3、快速创建SpringBoot应用之手工创建web应用...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_04.ssm整合之编写SpringMVC框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_5、SpringBoot2.x的依赖默认Maven版本...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_08.ssm整合之Spring整合MyBatis框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_汇总
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_2、SpringBoot2.x依赖环境和版本新特性说明...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_7、开发必备工具PostMan接口工具介绍和使用...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_4、快速创建SpringBoot应用之自动创建web应用...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_8、SpringBoot基础HTTP接口GET请求实战...
查看>>