Use Java to implement WeChat access and message push function of form data
Abstract:
This article introduces how to use Java programming language to implement WeChat access of form data Input and message push function. Through the API provided by the WeChat official account platform, we can integrate the form data filled in by users into the WeChat official account, and automatically send the data to the designated target through the message push function. This article will introduce how to use Java to write code to implement WeChat access to data and message push functions, and give corresponding code examples.
1. WeChat access configuration
2. Receive and verify messages
When the user enters relevant instructions in the WeChat official account, the WeChat server will send the received message to us in the configuration item in advance in the form of a POST request. Fill in the server URL. In order to receive and parse messages, we need to write Java code to implement the following functions:
public class WeChatServlet extends HttpServlet { private static final String TOKEN = "your_token"; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //验证请求是否来自微信服务器 String signature = request.getParameter("signature"); String timestamp = request.getParameter("timestamp"); String nonce = request.getParameter("nonce"); String echostr = request.getParameter("echostr"); String[] arr = {TOKEN, timestamp, nonce}; //字典排序 Arrays.sort(arr); StringBuilder sb = new StringBuilder(); for (String s : arr) { sb.append(s); } String tempStr = SHA1.encode(sb.toString()); //验证签名 if (tempStr.equals(signature)) { //接收并处理消息 //... } //返回验证结果 PrintWriter out = response.getWriter(); out.print(echostr); out.close(); } }
3. Message push
After receiving the message sent by the user, we need to automatically push the message to the specified target. Here we use access_token for authentication and use the message interface provided by the WeChat official account for message push.
public class AccessTokenUtil { private static final String APPID = "your_appid"; private static final String APPSECRET = "your_appsecret"; private static String access_token = null; private static long expires_time = 0; public static String getAccessToken() { if (access_token == null || System.currentTimeMillis() >= expires_time) { String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + APPID + "&secret=" + APPSECRET; String result = HttpUtil.sendGet(url); JSONObject jsonObject = JSONObject.parseObject(result); access_token = jsonObject.getString("access_token"); int expires_in = jsonObject.getIntValue("expires_in"); expires_time = System.currentTimeMillis() + (expires_in - 200) * 1000; } return access_token; } }
public class MessageUtil { public static void sendMessage(String openid, String message) { String url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + AccessTokenUtil.getAccessToken(); JSONObject jsonObject = new JSONObject(); jsonObject.put("touser", openid); jsonObject.put("msgtype", "text"); JSONObject text = new JSONObject(); text.put("content", message); jsonObject.put("text", text); String result = HttpUtil.sendPost(url, jsonObject.toJSONString()); } }
4. Form data integration and message push
public void handleMessage(HttpServletRequest request) { //获取用户输入的表单数据 String name = request.getParameter("name"); String email = request.getParameter("email"); String message = request.getParameter("message"); //整合表单数据 StringBuilder sb = new StringBuilder(); sb.append("姓名:").append(name).append(" "); sb.append("邮箱:").append(email).append(" "); sb.append("留言:").append(message); //将整合后的数据推送给目标 MessageUtil.sendMessage(target_openid, sb.toString()); }
Conclusion:
By using the Java programming language to implement the WeChat access and message push functions of form data, we can automatically fill in the form data Push to the specified target. This article gives corresponding code examples, I hope it will be helpful to everyone. Finally, you need to pay attention to obtaining access_token regularly to ensure the normal operation of message push.
The above is the detailed content of Using Java to implement WeChat access and message push functions for form data. For more information, please follow other related articles on the PHP Chinese website!