微信開發之(五)微信獲取自定義菜單

在處理請求的方法上,判斷是post的提交方式後加載代碼:html

 ///加載自定義菜單
                string postData = "{" + "\r\n";
                postData += "\"button\":[ " + "\r\n";
                postData += "{ " + "\r\n";
                postData += "\"name\":\"簡單查\"," + "\r\n";
                postData += "\"sub_button\":[" + "\r\n";
                postData += "{ " + "\r\n";
                postData += " \"type\":\"click\"," + "\r\n";
                postData += " \"name\":\"個人薪資\", " + "\r\n";
                postData += " \"key\":\"mypay\"" + "\r\n";
                postData += "}," + "\r\n";
                postData += "{ " + "\r\n";
                postData += " \"type\":\"click\"," + "\r\n";
                postData += " \"name\":\"天氣預報\", " + "\r\n";
                postData += " \"key\":\"tianqiyubao\"" + "\r\n";
                postData += "}," + "\r\n";
                postData += "{ " + "\r\n";
                postData += " \"type\":\"view\"," + "\r\n";
                postData += " \"name\":\"火車票查詢\", " + "\r\n";
                postData += " \"url\":\"http://www.baidu.com\"" + "\r\n";
                postData += "}," + "\r\n";
                postData += "{ " + "\r\n";
                postData += " \"type\":\"click\"," + "\r\n";
                postData += " \"name\":\"開心一刻\", " + "\r\n";
                postData += " \"key\":\"kaixinyixiao\"" + "\r\n";
                postData += " }]" + "\r\n";
                postData += "}," + "\r\n";
                postData += "{" + "\r\n";
                postData += "\"name\":\"會員管理\", " + "\r\n";
                postData += "\"sub_button\":[" + "\r\n";
                postData += "{ " + "\r\n";
                postData += " \"type\":\"view\"," + "\r\n";
                postData += " \"name\":\"會員註冊\", " + "\r\n";
                postData += " \"url\":\"http://www.vip.com\"" + "\r\n";
                postData += "}," + "\r\n";
                postData += "{ " + "\r\n";
                postData += " \"type\":\"view\"," + "\r\n";
                postData += " \"name\":\"重置密碼\", " + "\r\n";
                postData += " \"url\":\"http://www.taobao.com\"" + "\r\n";
                postData += "}," + "\r\n";
                postData += "{ " + "\r\n";
                postData += " \"type\":\"click\"," + "\r\n";
                postData += " \"name\":\"修改資料\", " + "\r\n";
                postData += " \"key\":\"updateMessage\"" + "\r\n";
                postData += "}," + "\r\n";
                postData += "{ " + "\r\n";
                postData += " \"type\":\"click\"," + "\r\n";
                postData += " \"name\":\"個人提問\", " + "\r\n";
                postData += " \"key\":\"mywen\"" + "\r\n";
                postData += "}," + "\r\n";
                postData += "{ " + "\r\n";
                postData += " \"type\":\"click\"," + "\r\n";
                postData += " \"name\":\"聯繫客服\", " + "\r\n";
                postData += " \"key\":\"PhoneSerices\"" + "\r\n";
                postData += " }]" + "\r\n";
                postData += "}," + "\r\n";
                postData += "{" + "\r\n";
                postData += "\"name\":\"活動通知\"," + "\r\n";
                postData += "\"sub_button\":[" + "\r\n";
                postData += "{ " + "\r\n";
                postData += " \"type\":\"click\"," + "\r\n";
                postData += " \"name\":\"近期活動\", " + "\r\n";
                postData += " \"key\":\"yuangonghuodong\"" + "\r\n";
                postData += "}," + "\r\n";
                postData += "{ " + "\r\n";
                postData += " \"type\":\"click\"," + "\r\n";
                postData += " \"name\":\"近期通知\", " + "\r\n";
                postData += " \"key\":\"yuangongtongzhi\"" + "\r\n";
                postData += "}," + "\r\n";
                postData += "{ " + "\r\n";
                postData += " \"type\":\"click\"," + "\r\n";
                postData += " \"name\":\"有問必答\", " + "\r\n";
                postData += " \"key\":\"youwenbida\"" + "\r\n";
                postData += " }]" + "\r\n";
                postData += "}]" + "\r\n";
                postData += "}" + "\r\n";

                //自定義菜單token的獲取 是用 下面的兩個參數 獲取的 不能直接用 公衆平臺的token
                string to = GetAccessToken();
                //本人不喜歡 後臺 json的操做 直接截取就能夠了 獲得的就是 token 或者 本身 獲取 json的token
                to = to.Substring(17, to.Length - 37);
                //加載菜單
                string i = GetPage("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + to, postData);

 

 /// <summary>
        /// 加載菜單項
        /// </summary>
        /// <param name="p"></param>
        /// <param name="postData"></param>
        /// <returns></returns>
        private string GetPage(string p, 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(p) 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;
                return content;
            }
            catch (Exception ex)
            {
                string err = ex.Message;
                return string.Empty;
            }
        }

 

        /// <summary>
        /// 獲取通行證
        /// </summary>
        /// <returns></returns>
        private string GetAccessToken()
        {
            string url_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx114078ffd1a092d5&secret=c76fc416e4d029e33e9c9ddb2d4bc9e3";
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url_token);
            myRequest.Method = "GET";
            HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
            StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
            string content = reader.ReadToEnd(); 
            reader.Close();
            return content;
        }
相關文章
相關標籤/搜索