微信开发之(五)微信获取自定义菜单
在处理请求的方法上,判断是post的提交方式后加载代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | ///加载自定义菜单 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); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | /// <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; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | /// <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; } |
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步