.NET微信公衆號開發-2.0建立自定義菜單

一.前言

開發以前,咱們須要閱讀官方的接口說明文檔,不得不吐槽一下,微信的這個官方文檔真的很爛,可是,爲了開發咱們須要的功能,咱們也不得不去看這些文檔.html

接口文檔地址:http://mp.weixin.qq.com/wiki/13/43de8269be54a0a6f64413e4dfa94f39.htmljson

看了這些個文檔,基本意思明白了,就是咱們把咱們要建立的菜單建立好,post到微信的服務器上面,微信服務器而後給咱們一些狀態碼,從而判斷咱們的菜單是否建立成功,只是在發送json數據之前咱們要作一 些身份驗證。api

二.準備工做

首先把咱們要建立的菜單寫在一個txt文本中:服務器

 {
     "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"
            }]
       }]
 }

三.開始編碼

  首先咱們建立一個通常處理程序createMenu.ashx.微信

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

  這裏須要注意的是appid,secret這些參數須要換成咱們本身的,這些參數咱們能夠放在配置文件中。也能夠單獨的放在一個幫助類裏面。cookie

     同時在建立菜單的時候咱們須要帶上個人access_token這個令牌來驗證咱們的身份,那麼咱們首先要作的就是獲取咱們的這個令牌,那個這個令牌要如何獲取了,咱們能夠經過一個接口獲取 ,只須要傳遞咱們的appid和secret這個兩個參數app

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

  上面的GetPage方法的返回值。這樣咱們就能夠獲取咱們的令牌了。post

      最後一步:帶上咱們的令牌,post咱們的json菜單數據就能夠建立菜單了。微信支付

      當你看到以下代碼:編碼

{"errcode":0,"errmsg":"ok"}

  說明你的菜單建立成功了。

四:GetPage

    代碼以下:

        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;
            }
        }

五.微信公衆號開發系列

1.0初始微信公衆號

2.0建立自定義菜單

3.0查詢自定義菜單

4.0公衆號消息處理

5.0微信支付

6.0模板消息

相關文章
相關標籤/搜索