java+ WeChat public Number development process steps detailed

Source: Internet
Author: User
Tags sha1 sha1 encryption stringbuffer
These two days I want to study the development of the public, so in the online search and practice, in the process because of various problems, (description is incomplete, some articles do not know how to jump out a new constant, do not say where the definition, and the jar package version of the problem pit my day time) so hereby in this record.

1. First, you have to register a public number, on the Niang search public platform, enter this page registration (skipped this step of the existing account):

2. Then create a new project with Eclipse, I'm building a Web project, and the JDK is 1.8,tomcat is 8.5.

3. In order to establish a connection, build a new class, named: Checkutil, which functions as a check tool for the connection, the code is as follows:

Import Java.security.messagedigest;import Java.util.arrays;public class Checkutil {public static final String Tooken = "from row definition "; The developer defines the Tookenpublic static Boolean checksignature (String signature,string timestamp,string nonce) {//1. Define array Storage Tooken , timestamp,noncestring[] arr = {tooken,timestamp,nonce};//2. Array to sort arrays.sort (arr);//3. Generate string StringBuffer SB = new StringBuffer (); for (String S:arr) {sb.append (s);} 4.SHA1 encryption, there are ready-made code String temp = GETSHA1 (sb.tostring ());//5. Compares the encrypted string with the encrypted signature that came, returns the result 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; }    }}

4. Then create a new servlet, overriding the Doget method to receive the sent GET request, some of the code is as follows:

public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { Response.setcontenttype ("text/html"); String signature = request.getparameter ("signature"); String timestamp = request.getparameter ("timestamp"); String nonce = Request.getparameter ("nonce"); String echostr = Request.getparameter ("Echostr"); PrintWriter out = Response.getwriter (), if (Checkutil.checksignature (signature, timestamp, nonce)) {//If the checksum is successful, Will get the random string of the original path back to Out.print (ECHOSTR);}}

5. After the code is finished, here need a tool to map our intranet link to the public, so as to access to our backstage, here I use a free mapping tool Ngrok, go to degree Niang search download can, after downloading the decompression placed in a specified location, while holding down the win key +r key, input cmd, Enter the DOS environment, switch to Ngrock, enter Ngrock http 8,080 to return the car (before this start Tomcat):

6. After the return to wait a while, you can get a link to the public network, the shaded area shown in the link, you can directly access the contents of the local 127.0.0.1:8080, respectively, the HTTP protocol and the HTTPS protocol corresponding address:

7. Login to the public number platform, swipe to the bottom, click on the bottom left corner of the development-basic configuration:

Click the Submit button to indicate that the submission is successful and that you have access to our own backstage.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.