1. Foreword
Before development, we need to read the official interface documentation. I have to complain that the official documentation of WeChat is really bad. However, in order to develop the functions we need, we also have to read these documents.
Interface document address: http://mp.weixin.qq.com/wiki/13/43de8269be54a0a6f64413e4dfa94f39.html
After reading these documents, I understand the basic meaning. We create the menu we want to create and post it to the WeChat server. The WeChat server then gives us some status codes to determine whether our menu is successfully created. We just need to do some authentication before sending json data.
2. Preparation
First write the menu we want to create in a txt text:
{ "button":[ { "type":"view", "name":"付停车费", "url":"http://www.baidu.com" },{ "name":"个人中心", "sub_button":[ { "type":"view", "name":"个人信息", "url":"http://www.baidu.com" }, { "type":"view", "name":"订单查询", "url":"http://www.baidu.com" }, { "type":"view", "name":"使用帮助", "url":"http://www.baidu.com" }, { "type":"view", "name":"下载APP", "url":"http://www.baidu.com" }] }] }
3. Start coding
First we create a general handler createMenu.ashx.
What needs to be noted here is that the appid and secret parameters need to be replaced with our own. We can put these parameters in the configuration file. It can also be placed separately in a helper class.
At the same time, when creating a menu, we need to bring my access_token to verify our identity. So the first thing we have to do is to get our token. How to get this token? We can Obtained through an interface, we only need to pass our appid and secret parameters
The return value of the GetPage method above. So we can get our token.
The last step: bring our token and post our json menu data to create the menu.
When you see the following code:
{"errcode":0,"errmsg":"ok"}
It means your menu is created successfully.
Four: 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); // 准备请求... try { // 设置参数 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(); //发送请求并获取相应回应数据 response = request.GetResponse() as HttpWebResponse; //直到request.GetResponse()程序才开始向目标网页发送Post请求 instream = response.GetResponseStream(); sr = new StreamReader(instream, encoding); //返回结果网页(html)代码 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 the entire content of this article, I hope you all like it