Create custom menu for. NET WeChat official account development,. net official account

Source: Internet
Author: User

Create custom menu for. NET public account development,. net public

I. Preface

Before development, we need to read the official interface instruction document and have to speak out. This official document is really bad, but in order to develop the functions we need, we also have to go to these documents.

Interface document address: http://mp.weixin.qq.com/wiki/13/43de8269be54a0a6f64413e4dfa94f39.html

After reading these documents, the basic meaning is clear, that is, we create the menu we want to create, post it to the server, and the server then gives us some status codes, to determine whether the menu is successfully created, we only need to perform some authentication before sending json data.

2. Preparations

First, write the menu we want to create in a txt text:

{"Button": [{"type": "view", "name": "Pay parking fee", "url": "http://www.baidu.com" },{ "name ": "Personal center", "sub_button": [{"type": "view", "name": "personal information", "url": "http://www.baidu.com "}, {"type": "view", "name": "Order query", "url": "http://www.baidu.com" },{ "type": "view", "name ": "Help", "url": "http://www.baidu.com" },{ "type": "view", "name": "Download APP", "url ": "http://www.baidu.com"}]}

Iii. Start Encoding

First, create a general handler createMenu. ashx.

Copy codeThe Code is as follows:
Public string access_token {get; set ;}
Protected void Page_Load (object sender, EventArgs e)
{
FileStream fs1 = new FileStream (Server. MapPath (".") + "\ menu.txt", FileMode. Open );
StreamReader sr = new StreamReader (fs1, Encoding. GetEncoding ("UTF-8 "));
String menu = sr. ReadToEnd ();
Sr. Close ();
Fs1.Close ();
Var str = GetPage ("https://api.weixin.qq.com/cgi-bin/token? Grant_type = client_credential & appid = wxd811f3164e3e56f3 & secret = 76eb33f66129692da16d148cb3c024f1 ","");
JObject jo = JObject. Parse (str );
Access_token = jo ["access_token"]. ToString ();
GetPage ("https://api.weixin.qq.com/cgi-bin/menu/create? Access_token = "+ access_token +" ", menu );
}

Here, we need to note that appid and secret must be replaced by our own parameters. These parameters can be placed in the configuration file. You can also put it in a help class separately.

At the same time, when creating the menu, we need to take the token of my access_token to verify our identity. The first thing we need to do is to get our token and how to get it, we can get it through an interface, just passing our appid and secret parameters

Copy codeThe Code is as follows:
{"Access_token": "jVLAT9Rp9dNgxI4pb4RWlSx_9HJLXICmk_uWDlRtAug8wcaWhZZ10eqZCYRZrEwCIJf1-vBhS9YEX00Dj7q _ lJCyTIWOxTruOd25opkf-0", "expires_in": 7200}

The returned value of the above GetPage method. In this way, we can get our token.

Last step: With our token, post our json menu data to create a menu.

When you see the following code:

{"Errcode": 0, "errmsg": "OK "}
This indicates that your menu has been created successfully.

4. GetPage

The Code is as follows:

Public string GetPage (string posturl, string postData) {Stream outstream = null; Stream instream = null; StreamReader sr = null; HttpWebResponse response = null; HttpWebRequest request = null; encoding encoding = Encoding. UTF8; byte [] data = encoding. getBytes (postData); // prepare the request... try {// set the parameter request = WebRequest. create (posturl) as HttpWebRequest; CookieContainer cookieContainer = new CookieContainer (); request. cookieContainer = cookieContainer; request. allowAutoRedirect = true; request. method = "POST"; request. contentType = "application/x-www-form-urlencoded"; request. contentLength = data. length; outstream = request. getRequestStream (); outstream. write (data, 0, data. length); outstream. close (); // send the request and obtain the response data response = request. getResponse () as HttpWebResponse; // wait until request. the GetResponse () program starts to send the Post request instream = response to the target webpage. getResponseStream (); sr = new StreamReader (instream, encoding); // return result webpage (html) code string content = sr. readToEnd (); string err = string. empty; Response. write (content); return content;} catch (Exception ex) {string err = ex. message; return string. empty ;}}

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

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.