Difficulties in Learning Java Wechat Public Number Development

Some time ago, when I was bored, I taught myself a little bit about the development of Wechat Public Number (threatened by my sister).

Address of Mu Course Online Course: http://www.imooc.com/learn/368 

After all, it's a free course. It can't be so detailed. So let me share with you the problems I have encountered.

This is the code I typed according to the course (IDE is Eclipse): https://github.com/Zuosy/WeiXin Send it to github.

For this, I went to Liao Da's website to study Git once.

Liao Da's Git Tutorial Transmitters: https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000

  • That mapping tool doesn't see urine

No, I used another mapping tool called natapp. Minasan can Baidu itself, there are pictures and textbooks on it.

Because it's for fun, a free one will do. This tool won't run out of time. It's good.

Send a picture show:

  • Basic configuration token validation failed

The main problem is that Laobi did not share the sha1 encryption algorithm. Baidu's possible Baidu to false "silly" encryption algorithm.

The portal of "Silly One" encryption algorithm: http://www.cnblogs.com/blackdeng/p/6060781.html

To prevent code loss, I post it.

Silly Encryption Algorithms Tool Class:

 1 package org.fc.Util;
 2 
 3 import java.security.MessageDigest;
 4 import java.security.NoSuchAlgorithmException;
 5 import java.util.Arrays;
 6 
 7 import com.sun.mail.handlers.message_rfc822;
 8 
 9 public class SignUtil {
10     
11     private static String  token="XXXXXX";
12     
13     /**
14      * Pass in three parameters and Wechat token verification.
15      * @param signature Signature is used to verify that the final result is consistent        
16      * @param timestamp Time stamp
17      * @param nonce Random Number Marker
18      * @return A Boolean value determines whether the final encryption is consistent with signature.
19      */
20     public static boolean checkSignature(String signature,
21             String timestamp,String nonce){
22         //Turn the incoming parameter into a String Arrays are then sorted by dictionary
23         String[] arr=new String[]{token,timestamp,nonce};
24         Arrays.sort(arr);
25         //Create an object to store the last three sorted objects String Union
26         StringBuilder content=new StringBuilder();
27         for(int i=0;i<arr.length;i++){
28             content.append(arr[i]);
29         }
30         
31         
32         //start-up sha1 Tools for Encryption
33         MessageDigest md=null;
34         String tmpStr=null;
35         try {
36             md=MessageDigest.getInstance("SHA-1");
37             //md.digest()Method must act on byte arrays
38             byte[] digest=md.digest(content.toString().getBytes());
39             //String byte arrays
40             tmpStr=byteToStr(digest);
41         } catch (NoSuchAlgorithmException e) {
42             // TODO Auto-generated catch block
43             e.printStackTrace();
44         }
45         content=null;
46         
47         return tmpStr!=null?tmpStr.equals(signature.toUpperCase()):false;
48         
49     }
50     
51     
52     /**
53      * Processing bytes and converting them into strings
54      * @param digest
55      * @return
56      */
57     private static String byteToStr(byte[] digest){
58         String strDigest="";
59         for(int i=0;i<digest.length;i++){
60             //Converting the binary code of the character to the code-digit string of the hexadecimal code
61             strDigest+=byteToHexStr(digest[i]);
62         }
63         return strDigest;
64     }
65     
66     /**
67      * Processing each byte into a 16-bit string
68      * @param b
69      * @return
70      */
71     public static String byteToHexStr(byte b){
72         //Translational Reference Table
73         char[] Digit= {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
74         
75         
76         //Bit operation converts binary to hexadecimal
77         char[] tempArr=new char[2];
78         tempArr[0]=Digit[(b>>>4)&0X0F];//XXXX&1111 So what we got was XXXX
79         tempArr[1]=Digit[b&0X0F];//XXXX&1111 So what we got was XXXX
80         
81         //Get the string of the binary code
82         String s=new String(tempArr);
83         return s;
84     }
85 }

This is a good one. You can replace Check Util directly.

  • XStream error reporting

Lack of jar packages for xmlpull

Portal: http://blog.csdn.net/ljg888/article/details/7711852

In fact, those jar packages can be downloaded on the internet, and the gitbug project I sent out should already be there.

  • Chinese Scrambling Problem

This is due to my carelessness.

1 response.setCharacterEncoding("UTF-8");

This line of code must be written in the first line of the doPost method.

  • Pay attention to case and case

Note that the attributes (variable names) in the textMessage class must be case-sensitive. Otherwise, when it's encapsulated in xml, you'll hit GG.

  • MessageUtil Tool Class

At the time of development, I found that this tool class can be recycled. I'll post it. It'll be used later.

 1 package com.misaka.util;
 2 
 3 import java.io.IOException;
 4 import java.io.InputStream;
 5 import java.util.Date;
 6 import java.util.HashMap;
 7 import java.util.List;
 8 import java.util.Map;
 9 import javax.servlet.http.HttpServletRequest;
10 import org.dom4j.Document;
11 import org.dom4j.DocumentException;
12 import org.dom4j.Element;
13 import org.dom4j.io.SAXReader;
14 
15 import com.misaka.po.TextMessage;
16 import com.thoughtworks.xstream.XStream;
17 
18 public class MessageUtil {
19 
20     // Pay attention to case and case
21     public static final String MESSAGE_TEXT = "text";
22     public static final String MESSAGE_IMAGE = "image";
23     public static final String MESSAGE_VOICE = "voice";
24     public static final String MESSAGE_VIDEO = "video";
25     public static final String MESSAGE_SHORTVIDEO = "shortvideo";
26     public static final String MESSAGE_LINK = "link";
27     public static final String MESSAGE_LOCATION = "location";
28     // Event
29     public static final String MESSAGE_EVENT = "event";
30     public static final String EVENT_SUBSCRIBE = "subscribe";
31     public static final String EVENT_UNSUBSCRIBE = "unsubscribe";
32     public static final String EVENT_SCAN = "SCAN";// Sweep code
33     public static final String EVENT_LOCATION = "LOCATION";// Report geographic location
34     public static final String EVENT_CLICK = "CLICK";
35     public static final String EVENT_VIEW = "VIEW";
36 
37     /*
38      * You need an XML - > map method and a map - > XML method
39      */
40 
41     /**
42      * xml --> map
43      * @param request
44      * @return
45      * @throws IOException
46      * @throws DocumentException
47      */
48     public static Map<String, String> xmlToMap(HttpServletRequest request) throws IOException, DocumentException {
49         Map<String, String> map = new HashMap<>();
50         SAXReader reader = new SAXReader();
51         InputStream input = request.getInputStream();
52 
53         Document doc = reader.read(input);
54         Element root = doc.getRootElement();
55         List<Element> list = root.elements();
56 
57         for (Element e : list) {
58             map.put(e.getName(), e.getText());
59         }
60         input.close();
61         return map;
62     }
63 
64     /**
65      * textMessage --> xml
66      * @param textMessage
67      * @return
68      */
69     public static String textMessageToXml(TextMessage textMessage) {
70         XStream xstream = new XStream();
71         xstream.alias("xml", textMessage.getClass());
72         return xstream.toXML(textMessage);
73     }
74 
75     public static String initText(String toUserName, String fromUserName, String content) {
76         TextMessage text = new TextMessage();
77         text.setFromUserName(toUserName);
78         text.setToUserName(fromUserName);
79         text.setMsgType(MESSAGE_TEXT);
80         text.setCreateTime(new Date().getTime());
81         text.setContent(content);
82         return textMessageToXml(text);
83     }
84 
85 }

 

 

 

This is my handwriting on the Mucho website: http://www.imooc.com/article/20238 

Don't use Mu's handwriting on the Internet. Don't report me plagiarizing.

Keywords: Java xml github git

Added by daveh33 on Sat, 25 May 2019 22:38:13 +0300