search
HomeWeChat AppletWeChat DevelopmentUsing asp.net to develop WeChat public platform to obtain user messages and process them

This article mainly introduces the use of asp.net For relevant information on how to develop WeChat public platform to obtain user messages and process them, friends in need can refer to

Getting user messages

The messages sent by users are on the WeChat server Contained in an HTTP POST request sent, obtaining the message sent by the user must be obtained from the data stream of the POST request

Example of HTTP request message for the WeChat server to push the message to the server

POST /cgi-bin/wxpush? msg_signature=477715d11cdb4164915debcba66cb864d751f3e6&timestamp=1409659813&nonce=1372623149 HTTP/1.1

Host: qy.weixin.qq .com

Get data from POST request

Using asp.net to develop WeChat public platform to obtain user messages and process them

#The user message obtained in this way may be in two situations: encrypted message or unencrypted message. This is related to the message encryption and decryption mode you selected when configuring the website on the WeChat public platform. If you select plaintext mode, it will not be encrypted. If you select compatibility mode, both ciphertext and plaintext will exist. If you select Safe mode, user messages will be encrypted and need to be decrypted before further processing

2. Reply to user messages

Refer to WeChat public platform development documentation

•Text message

<xml> 
<ToUserName><![CDATA[{0}]]></ToUserName> 
<FromUserName><![CDATA[{1}]]></FromUserName> 
<CreateTime>{2}</CreateTime> 
<MsgType><![CDATA[text]]></MsgType> 
<Content><![CDATA[{3}]]></Content> 
</xml>


##•

PictureMessage

<xml> 
<ToUserName><![CDATA[{0}]]></ToUserName> 
<FromUserName><![CDATA[{1}]]></FromUserName> 
<CreateTime>{2}</CreateTime> 
<MsgType><![CDATA[image]]></MsgType> 
<Image> 
<MediaId><![CDATA[{3}]]></MediaId> 
</Image> 
</xml>

The message format is already there, then we just need to set the corresponding parameters

responseContent = string.Format(ReplyType.Message_Text, 
 FromUserName.InnerText, 
 ToUserName.InnerText, 
DateTime.Now.Ticks, 
String.IsNullOrEmpty(reply)?"Sorry,I can not follow you." :reply);

3. Encryption and decryption of user messages and server messages

The WeChat public platform developer documentation provides encryption and decryption examples in various languages ​​such as c++,

C#,java, etc. We are using C#. We only need to add two of the files to the project. Sample.cs is the sample code given by the WeChat team. There is no need to reference . For ## Just add references to the #WXBizMsg

Crypt

.cs and Cryptography.cs files. In order to further encapsulate and facilitate calling, I created a new class WeChatSecurityHelper and defined two methods respectively. To encrypt (EncryptMsg) and decrypt (DecryptMsg), create a WXBizMsgCrypt

object

, call its method to encrypt and decrypt, the specific code can be seen in the code example

Using asp.net to develop WeChat public platform to obtain user messages and process them WeChatSecurityHelper


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common
{
  public class WeChatSecurityHelper
  {
    /// <summary>
    /// 定义Token,与微信公共平台上的Token保持一致
    /// </summary>
    private const string Token = "StupidMe";
    /// <summary>
    /// AppId 要与 微信公共平台 上的 AppId 保持一致
    /// </summary>
    private const string AppId = "11111111111";
    /// <summary>
    /// 加密用 
    /// </summary>
    private const string AESKey = "pvX2KZWRLQSkUAbvArgLSAxCwTtxgFWF3XOnJ9iEUMG";

    private static Tencent.WXBizMsgCrypt wxcpt = new Tencent.WXBizMsgCrypt(Token, AESKey, AppId);
    private string signature,timestamp,nonce;
    private static LogHelper logger = new LogHelper(typeof(WeChatSecurityHelper));


    public WeChatSecurityHelper(string signature, string timestamp, string nonce)
    {
      this.signature = signature;
      this.timestamp = timestamp;
      this.nonce = nonce;
    }

    /// <summary>
    /// 加密消息
    /// </summary>
    /// <param name="msg">要加密的消息</param>
    /// <returns>加密后的消息</returns>
    public string EncryptMsg(string msg)
    {
      string encryptMsg="";
      int result = wxcpt.EncryptMsg(msg, timestamp, nonce, ref encryptMsg);
      if (result == 0)
      {
        return encryptMsg;
      }
      else
      {
        logger.Error("消息加密失败");
        return "";
      }
    }

    /// <summary>
    /// 解密消息
    /// </summary>
    /// <param name="msg">消息体</param>
    /// <returns>明文消息</returns>
    public string DecryptMsg(string msg)
    {
      string decryptMsg = "";
      int result = wxcpt.DecryptMsg(signature, timestamp, nonce, msg,ref decryptMsg);
      if (result != 0)
      {
        logger.Error("消息解密失败,result:"+result);
      }
      return decryptMsg;
    }
  }
}

The above is the entire content of this article, I hope you all like it.

The above is the detailed content of Using asp.net to develop WeChat public platform to obtain user messages and process them. 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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software