微信公众号java开发教程

时间:2022-08-31 18:09:03

详见LAO BI慕课网视频

使用servlet + tomcat 开发

准备外网映射

使用ngrok软件,作用:
让本地java web项目能够通过外网访问,方便调试

由于该软件是国外的,所以服务器不一定能访问,有一个基于国内服务器的ngrok->地址

下载解压后,通过cmd进到ngrok.exe的所在文件夹,输入

ngrok -config=ngrok.cfg -subdomain xxx 80

xxx是你自己起的名字,然后你就可以通过

xxx.tunnel.qydev.com

访问你的本地80端口

注意
1. 你的tomcat的端口不一定是80端口,比如我的是8080,那么上面的80就要改成8080
2. cmd窗口打开设置好后,如果关了映射失效

Servlet编写

服务器验证

微信服务器的验证要求(详见微信平台的开发者文档):
微信通过通过get请求传进四个参数

signature   微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
timestamp 时间戳
nonce 随机数
echostr 随机字符串

开发者通过检验signature对请求进行校验(下面有校验方式)。若确认此次GET请求来自微信服务器,请原样返回echostr参数内容,则接入生效,成为开发者成功,否则接入失败。加密/校验流程如下:
1)将token、timestamp、nonce三个参数进行字典序排序
2)将三个参数字符串拼接成一个字符串进行sha1加密
3)开发者获得加密后的字符串可与signature对比,标识该请求来源于微信

其中 token是自定的,完成代码

Servlet里面:

public void doGet(HttpServletRequest req, HttpServletResponse response)
throws ServletException, IOException {

String signature = req.getParameter("signature");
String timestamp = req.getParameter("timestamp");
String nonce = req.getParameter("nonce");
String echostr = req.getParameter("echostr");

PrintWriter out = response.getWriter();
if(CheckUtil.checkSignature(signature, timestamp, nonce)){
out.print(echostr);
}
}

CheckUtil的代码:

public class CheckUtil {

private static final String token = "ggggg";

public static boolean checkSignature(String signature,String timestamp,String nonce){
String[] arr = new String[]{token,timestamp,nonce};
Arrays.sort(arr);

StringBuffer content = new StringBuffer();

for (int i = 0; i < arr.length; i++) {
content.append(arr[i]);
}
String temp = getSha1(content.toString());
return temp.equals(signature);
}

public static String getSha1(String str){
if(str==null||str.length()==0){
return null;
}
char hexDigits[] = {'0','1','2','3','4','5','6','7','8','9',
'a','b','c','d','e','f'};
try {
MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
mdTemp.update(str.getBytes("UTF-8"));

byte[] md = mdTemp.digest();
int j = md.length;
char buf[] = new char[j*2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
buf[k++] = hexDigits[byte0 & 0xf];
}
return new String(buf);
} catch (Exception e) {
// TODO: handle exception
return null;
}
}
}

消息回复

微信通信用的是XML,所以我们要新建一个javabean:

public class TextMessage {
private String ToUserName;
private String FromUserName;
private long CreateTime;
private String MsgType;
private String Content;
private long MsgId;
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 l) {
CreateTime = l;
}
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 long getMsgId() {
return MsgId;
}
public void setMsgId(long msgId) {
MsgId = msgId;
}
}

xml和javabean的互相转换
MessageUtil:

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 EVENT_SUB = "subscribe";
public static final String EVENT_UNSUB = "unsubscribe";
public static final String EVENT_CLICK = "CLICK";
public static final String EVENT_VIEW = "VIEW";

/**
* xml转为map
* @param request
* @return
* @throws DocumentException
* @throws IOException
*/

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();
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;

}

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(MESSAGE_TEXT);
text.setCreateTime(new Date().getTime());
text.setContent(content);
return textMessageToXml(text);
}

public static String menuText(){
StringBuffer sb = new StringBuffer();

sb.append("谢谢关注");
return sb.toString();
}

注意导包

dom4j包 用来解析xml
xstream包 用来封装类
xmlpull包(如果xstream出现无法找到xmlpull类就就要导入该包)

写dopost方法:

req.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
try {
Map<String,String> map = MessageUtil.xmlToMap(req);
String toUserName = map.get("ToUserName");
String fromUserName = map.get("FromUserName");
String msgType = map.get("MsgType");
String content = map.get("Content");

String message = null;
if(MessageUtil.MESSAGE_TEXT.equals(msgType))
{
String mycontent = "你回复的消息是: " + content;
message = MessageUtil.initText(toUserName, fromUserName, mycontent);

}else if(MessageUtil.MESSAGE_EVENT.equals(msgType)){
String eventType = map.get("Event");
if(MessageUtil.EVENT_SUB.equals(eventType)){
String mycontent = MessageUtil.menuText();
message = MessageUtil.initText(toUserName, fromUserName, mycontent);

}
}
out.print(message);

} catch (Exception e) {
// TODO: handle exception
out.close();
}
}