慕课网消息的接收与响应2

还是不行

 

可以了

贴一下代码吧:

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)){
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);
}
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
}

}

MessageUtil.java

package com.imooc.util;

import java.io.IOException;
import java.io.InputStream;
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.imooc.po.TextMessage;
import com.thoughtworks.xstream.XStream;

public class MessageUtil {
/**
* 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);
}





}

 

TextMessage.java

package com.imooc.po;
public class TextMessage {
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位整型





}

posted on 2017-01-11 08:38  绿茵好莱坞  阅读(182)  评论(0编辑  收藏  举报

导航