search
HomeJavajavaTutorialDetailed explanation of Java+WeChat public account development process steps

In the past two days, I wanted to learn WeChat public account development, so I searched online and started practicing. During the process, due to various problems, (the description was incomplete, some articles popped up a new constant without knowing how, It didn’t say where it was defined, and the problem with the jar package version cost me a day), so I will record it here.

1. First, you must register a WeChat public account. Go to Du Niang to search the WeChat public platform and enter this page to register (skip this step if you already have an account):

Detailed explanation of Java+WeChat public account development process steps

2. Then use Eclipse to create a new project. Here I am building a web project, jdk is 1.8, and tomcat is 8.5.

Detailed explanation of Java+WeChat public account development process steps

3 .In order to establish a connection with WeChat, after building the project, first create a new class and name it: CheckUtil. Its function is to serve as a verification tool for connecting to WeChat. The code is as follows:

import java.security.MessageDigest;
import java.util.Arrays;

public class CheckUtil {
	public static final String  tooken = "自行定义"; //开发者自行定义Tooken
	public static boolean checkSignature(String signature,String timestamp,String nonce){
		//1.定义数组存放tooken,timestamp,nonce
		String[] arr = {tooken,timestamp,nonce};
		//2.对数组进行排序
		Arrays.sort(arr);
		//3.生成字符串
		StringBuffer sb = new StringBuffer();
		for(String s : arr){
			sb.append(s);
		}
		//4.sha1加密,网上均有现成代码
		String temp = getSha1(sb.toString());
		//5.将加密后的字符串,与微信传来的加密签名比较,返回结果
		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 , rewrite the doGet method to receive the GET request from WeChat. Part 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)){
			//如果校验成功,将得到的随机字符串原路返回
			out.print(echostr);
		}
	}

5. After the code is written, we need a tool to map our intranet link to the public network so that WeChat can access it. Go to our backend. Here I use ngrok, a free mapping tool. Just go to Du Niang to search and download it. After downloading, unzip it and put it in a designated location. Press the Win key and R key at the same time, enter cmd, and press Enter to enter. Dos environment, switch to the drive letter where ngrock is located, enter ngrock http 8080 and press Enter (start tomcat before doing this):

Detailed explanation of Java+WeChat public account development process steps

6. Press Enter and wait for a while, that is You can get the public network link. The link given in the shaded area shown in the figure below can directly access the link content under the local machine 127.0.0.1:8080, which are the addresses corresponding to the http protocol and https protocol:

Detailed explanation of Java+WeChat public account development process steps

7. Log in to the WeChat public account platform, slide to the bottom, and click Development in the lower left corner - Basic configuration:

Detailed explanation of Java+WeChat public account development process steps

Click the submit button , the prompt submission is successful, which means that WeChat can access our own backend.

Related recommendations:

Java implements graphic and text code examples for WeChat public platform development

WeChat public account payment development (java) examples Detailed explanation

The above is the detailed content of Detailed explanation of Java+WeChat public account development process steps. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

How can I implement functional programming techniques in Java?How can I implement functional programming techniques in Java?Mar 11, 2025 pm 05:51 PM

This article explores integrating functional programming into Java using lambda expressions, Streams API, method references, and Optional. It highlights benefits like improved code readability and maintainability through conciseness and immutability

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I use Java's NIO (New Input/Output) API for non-blocking I/O?How do I use Java's NIO (New Input/Output) API for non-blocking I/O?Mar 11, 2025 pm 05:51 PM

This article explains Java's NIO API for non-blocking I/O, using Selectors and Channels to handle multiple connections efficiently with a single thread. It details the process, benefits (scalability, performance), and potential pitfalls (complexity,

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

How do I use Java's sockets API for network communication?How do I use Java's sockets API for network communication?Mar 11, 2025 pm 05:53 PM

This article details Java's socket API for network communication, covering client-server setup, data handling, and crucial considerations like resource management, error handling, and security. It also explores performance optimization techniques, i

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function