Use. NET to develop a custom menu for WeChat official accounts

Source: Internet
Author: User
This article mainly introduces. for more information about how to create a custom menu, see the following article. for more information about how to create a custom menu by using the. NET Developer account, see.

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:


        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=wxd811f5114e3e56f3&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);        }


III. start encoding

First, create a general handler createMenu. ashx.

The 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=wxd811f5114e3e56f3&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


The code is as follows:

{"access_token":"jVLAT9Rp9dNgxI4pb4RWlSx_9HJLXICmk_uWDlRtAug8wcaWhZZ10eqZCYRZrEwCIJf1-vBhS9YEX00Dj7qlJCyTIWOxTruOd25opkf-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.

The preceding section describes how to use. NET to develop a public account to create a custom menu. For more information, see other related articles on php Chinese network!

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.