Home  >  Article  >  Web Front-end  >  .NET WeChat public account development to create custom menu_javascript skills

.NET WeChat public account development to create custom menu_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:50:001477browse

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.

Copy code 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);
}

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

Copy code The code is as follows:

{"access_token":"jVLAT9Rp9dNgxI4pb4RWlSx_9HJLXICmk_uWDlRtAug8wcaWhZZ10eqZCYRZrEwCIJf1-vBhS9YEX00Dj7q__lJCyTIWOxTruOd25opkf-0","expires_in":7200}

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

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