博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#微信公众号——自定义菜单
阅读量:4884 次
发布时间:2019-06-11

本文共 5646 字,大约阅读时间需要 18 分钟。

自定义菜单最多包括3个一级菜单,每个一级菜单最多包含5个二级菜单。一级菜单最多4个汉字,二级菜单最多7个汉字,多出来的部分将会以“...”代替。 自定义菜单的介绍,可以看官方开发文档http://mp.weixin.qq.com/wiki/home/index.html 下面是我自己写的一个实现自定义菜单的小Dome 一、先定义了一个Json字符串,格式要按照微信提供的Json格式
//创建微信菜单JSON字符串        private string GetWXMenuStr()        {            string weixin1 = "";            weixin1 += "{\n";            weixin1 += "\"button\":[\n";            weixin1 += "{\n";            // weixin1 += "\"type\":\"click\",\n";            //第一个菜单            weixin1 += "\"name\":\"公共信息\",\n";            weixin1 += "\"sub_button\":[\n";            weixin1 += "{\n";            weixin1 += "\"type\":\"click\",\n";            weixin1 += "\"name\":\"通知公告\",\n";            weixin1 += "\"key\":\"11\"\n";            weixin1 += "},\n";            weixin1 += "{\n";            weixin1 += "\"type\":\"click\",\n";            weixin1 += "\"name\":\"工作动态\",\n";            weixin1 += "\"key\":\"12\"\n";            weixin1 += "},\n";            weixin1 += "{\n";            weixin1 += "\"type\":\"click\",\n";            weixin1 += "\"name\":\"政策法规\",\n";            weixin1 += "\"key\":\"13\"\n";            weixin1 += "},\n";            weixin1 += "{\n";            weixin1 += "\"type\":\"click\",\n";            weixin1 += "\"name\":\"经济视野\",\n";            weixin1 += "\"key\":\"14\"\n";            weixin1 += "},\n";            weixin1 += "{\n";            weixin1 += "\"type\":\"click\",\n";            weixin1 += "\"name\":\"专题报道\",\n";            weixin1 += "\"key\":\"15\"\n";            weixin1 += "}]\n";            weixin1 += "},\n";            //第二个菜单            weixin1 += "{\n";            //weixin1 += "\"type\":\"click\",\n";            weixin1 += "\"name\":\"公共服务\",\n";            weixin1 += "\"sub_button\":[\n";            weixin1 += "{\n";            weixin1 += "\"type\":\"click\",\n";            weixin1 += "\"name\":\"企业之窗\",\n";            weixin1 += "\"key\":\"21\"\n";            weixin1 += "},\n";            weixin1 += "{\n";            weixin1 += "\"type\":\"click\",\n";            weixin1 += "\"name\":\"金融服务\",\n";            weixin1 += "\"key\":\"22\"\n";            weixin1 += "},\n";            weixin1 += "{\n";            weixin1 += "\"type\":\"click\",\n";            weixin1 += "\"name\":\"创业指导\",\n";            weixin1 += "\"key\":\"23\"\n";            weixin1 += "},\n";            weixin1 += "{\n";            weixin1 += "\"type\":\"click\",\n";            weixin1 += "\"name\":\"管理服务\",\n";            weixin1 += "\"key\":\"24\"\n";            weixin1 += "},\n";            weixin1 += "{\n";            weixin1 += "\"type\":\"click\",\n";            weixin1 += "\"name\":\"法律服务\",\n";            weixin1 += "\"key\":\"25\"\n";            weixin1 += "}]\n";            weixin1 += "},\n";            //第三个菜单(view类型的)            weixin1 += "{\n";            weixin1 += "\"name\":\"互动交流\",\n";            weixin1 += "\"sub_button\":[\n";            weixin1 += "{\n";            weixin1 += "\"type\":\"view\",\n";            weixin1 += "\"name\":\"注册\",\n";            weixin1 += "\"url\":\"http://1532618rl0.iok.la/Register.aspx\"\n";            weixin1 += "},\n";            weixin1 += "{\n";            weixin1 += "\"type\":\"view\",\n";            weixin1 += "\"name\":\"登录\",\n";            weixin1 += "\"url\":\"http://1532618rl0.iok.la\"\n";            weixin1 += "}]\n";            weixin1 += "}\n";            weixin1 += "}]\n";            weixin1 += "}\n";            return weixin1;        }

二、定义执行菜单请求方法

///          /// 执行菜单请求         ///          /// 微信提供的自定义菜单接口地址         /// 实现自定义菜单的Json字符串         public void PostMenuData(string url, 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(url) 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;             }             catch (Exception ex)             {
                string err = ex.Message;             }         }

三、实现自定义菜单

private string postUrl = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=";        //创建自定义菜单        public void CreateWxMenu()        {            string weixin1 = GetWXMenuStr();//获取菜单Json字符串            string AccessToken = Helper.IsExistAccess_Token();//获取AccessToken            PostMenuData(postUrl + AccessToken, weixin1);//通过接口实现自定义菜单        }

四、在程序运行的起始页调用CreateWxMenu()方法即可。

其中获取AccessToken的方法是调用前面写的文章封装的一个Helper方法实现的,地址http://www.cnblogs.com/zhangjd/p/5694737.html

转载于:https://www.cnblogs.com/zhangjd/p/5697869.html

你可能感兴趣的文章
用户交互程序,格式化输出
查看>>
SPOJ PT07X Vertex Cover
查看>>
$ python-json模块的基本用法
查看>>
5.6.3.4 trim()方法
查看>>
SQL演练
查看>>
React Antd中样式的修改
查看>>
Spring 应用外部属性文件 配置 context 错误
查看>>
导入lxml找不到etree,报ImportError:DLL load failed:找不到指定的程序
查看>>
面向对象一
查看>>
大象的崛起!Hadoop七年发展风雨录
查看>>
图片二值化
查看>>
数据库常用函数
查看>>
集合之TreeSet(含JDK1.8源码分析)
查看>>
C语言学习的记忆
查看>>
Lucene学习总结之三:Lucene的索引文件格式(1) 2014-06-25 14:15 1124人阅读 ...
查看>>
node-sass 报错的解决方法
查看>>
Python:GeoJson格式的多边形裁剪Tiff影像并计算栅格数值
查看>>
免费下载知网文献的方法 | sci-hub免费下载SCI论文方法
查看>>
测试用例,变量之间,相互调用的方法,和修改原来初始化变量的方法
查看>>
ASP.NET MVC中将控制器分离到类库的实现(转)
查看>>