. Net WeChat official account development-Quick Start and. net Quick Start

Source: Internet
Author: User

. Net public account development-Quick Start and. net Quick Start

Author: Wang Xianrong

Recently, I was studying public account development and made the learning achievements into a class library to facilitate reuse.

The following results are obtained by using the flow of this class library and a few lines of code.

1. reference the public account class library

Reference xrwang. weixin. PublicAccount.

 

2. Add Public Account Information

Add the public account that provides the service, including the original id, AppId, AppSecret, and EncodingAESKey. The Code is as follows:

AccountInfoCollection.SetAccountInfo(new AccountInfo("YourOriginalId", "AppId", "AppSecret", "Token", "EncodingAESKey"));

If you need to provide services for multiple public accounts on one site of a server at the same time, repeat the above line of code.

(Note: the tokens of multiple public accounts must be consistent, which is the reason for the server)

I like to add public account information to the Application_Start method of Gobal. asax.

 

3. Communicate with the server

I added a general processing page named "WeixinInterface. ashx" and communicated with the server, including verifying the request, processing the request, and replying to the appropriate response. The Code is as follows:

Public void ProcessRequest (HttpContext context) {string result = string. empty; if (Validate (context) {if (context. request. httpMethod = WebRequestMethods. http. get) result = HandleGet (context); else if (context. request. httpMethod = WebRequestMethods. http. post) result = HandlePost (context);} else Message. insert (new Message (MessageType. exception, "An error occurred while verifying the message. \ R \ n address: "+ context. Request. RawUrl); context. Response. Write (result );}Communicate with the server

3.1 Verification request

First, check whether the received request comes from the server. The method is as follows:

/// <Summary> /// verify the message validity /// </summary> /// <param name = "context"> </param> /// <returns> If the message is valid, returns true; otherwise, returns false. </Returns> private bool Validate (HttpContext context) {string token = AccountInfoCollection. first. token; // because no public account is input during signature verification, the TOKEN string signature = RequestEx of the first public account is used here. tryGetQueryString ("signature"); string timestamp = RequestEx. tryGetQueryString ("timestamp"); string nonce = RequestEx. tryGetQueryString ("nonce"); if (string. isNullOrWhiteSpace (signature) | string. isNullOrWhiteSpace (timestamp) | string. isNullOrWhiteSpace (nonce) return false; return xrwang. weixin. publicAccount. utility. checkSignature (signature, token, timestamp, nonce );}Verification request

Of course, if you are full of love for the world, believe that there is no deception; if you are strict with the economy, you need to improve performance urgently; it is also possible not to verify.

3.2 process requests

After verifying the request, we can process the request in two ways:

(1) The GET request of the server is used to verify whether our server is working. We can directly return echostr;

/// <Summary> /// GET request processed, verify the signature /// </summary> /// <param name = "context"> </param> /// <returns> echostr </returns> private string HandleGet (httpContext context) {return RequestEx. tryGetQueryString ("echostr ");}Process GET requests

(2) the POST request of the server. This is the message that the Server delivers to us. We need to parse the message.

RequestMessageHelper helper = new RequestMessageHelper(context.Request);

3.3 Reply

After resolving the messages sent to us by the server, we need to respond. I will send the received message back directly, so I am lazy ~ \ (Too many rows )/~ La la

/// <Summary> /// the POST request to be processed /// </summary> /// <param name = "context"> </param> /// <returns> return xml response </returns> private string HandlePost (HttpContext context) {RequestMessageHelper helper = new RequestMessageHelper (context. request); if (helper. message! = Null) {ResponseBaseMessage responseMessage = HandleRequestMessage (helper. message); return responseMessage. toXml (helper. encryptType);} else return string. empty;} // <summary> // process the request message, return response Message // </summary> /// <returns> return response Message </returns> private ResponseBaseMessage HandleRequestMessage (RequestBaseMessage requestMessage) {ResponseTextMessage response = new ResponseTextMessage (requestMessage. fromUserName, requestMessage. toUserName, DateTime. now, string. format ("automatic reply, request content: \ r \ n {0}", requestMessage); response. content + = "\ r \ n <a href = \" http://www.cnblogs.com \ "> blog </a>"; return response ;}Response

Of course, under normal circumstances, we need to defend ourselves against attacks and respond to different requests. If you need to queue the request and then reply to the customer service one by one, you can directly reply to the empty string. For details about how to reply to the Customer Service Message, refer to the following article.

 

4. Introduction to the public account class library

Xrwang. weixin. PublicAccount is a set of class libraries that simplify the development of public accounts. It is developed by Wang Xianrong and is being added. Using the MIT open-source protocol, you can use it as you like. Don't delete my name.

Source Code address: http://git.oschina.net/xrwang2/xrwang.weixin.PublicAccount

If a BUG is found, please leave a message in your blog or send me an email: xrwang (a) 126.com.

Do not use QQ or Alibaba trademanager to chat, disturb me to play games, I will swear>. <

 

5. Test number

The following are my test and public accounts. You can compare them with the article.

Test No.

 

The test code has many permissions and can test almost all functions of the public platform.

My public account

Xrwang

 

The personal subscription number has fewer functions, but I will optimize it.

 

Thank you for reading this article and hope to help you. This article is from xrwang's blog http: // xrwang/cnblogs.com. You are welcome to repost it without tampering with the author to spread your knowledge.

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.