首先,个人订阅号没有自定义菜单的权限,这时候可以使用测试公众号
步骤:定义json文件
复制代码
具体的json字段含义可以看
http://mp.weixin.qq.com/wiki/10/0234e39a2025342c17a7d23595c6b40a.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20{ "button": [ { "type": "click", "name": "菜单一", "key": "one" }, { "type": "click", "name": "菜单二", "key": "two" }, { "type": "click", "name": "菜单三", "key": "three" } ] }
然后josn数据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
84
85
86
87
88
89
90
91/// <summary> /// 创建自定义菜单 /// </summary> /// <returns></returns> public string createMenu() { try { Hashtable hash = new Hashtable(); hash.Add("access_token", access_token()); string menu = readJson(Server.MapPath("..") + "\menu.json"); string result = Util.HttpRequestPostBody("https://api.weixin.qq.com/cgi-bin/menu/create", hash, menu); Hashtable hr = JsonConvert.DeserializeObject<Hashtable>(result); if (hr["errcode"].ToString().ToInt32() != 0) { throw new Exception(hr["errmsg"].ToString()); } return hr["errmsg"].ToString(); } catch (Exception x) { return x.Message; } } private string readJson(string jsonPath) { FileStream fs1 = new FileStream(jsonPath, FileMode.Open); StreamReader sr = new StreamReader(fs1, Encoding.GetEncoding("GBK")); string json = sr.ReadToEnd(); sr.Close(); fs1.Close(); return json; } /// <summary> /// 访问微信接口 包含body参数的函数 /// </summary> /// <param name="url">绝对url</param> /// <param name="hash">除body以外的参数</param> /// <param name="postData">要post的数据,body参数值</param> /// <returns></returns> public static string HttpRequestPostBody(string url, Hashtable hash, string postData = "") { List<string> post = new List<string>(); foreach (DictionaryEntry d in hash) { post.Add(d.Key + "=" + d.Value); } url = url + "?" + string.Join("&", post); 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; if (postData == "") { request.Method = "GET"; } else { 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(); return content; } catch (Exception ex) { string err = ex.Message; return string.Empty; } }
access_token()函数是访问微信得到,可查看http://mp.weixin.qq.com/wiki/14/9f9c82c1af308e3b14ba9b973f99a8ba.html
我这里JsonConvert.DeserializeObject<Hashtable>(result)使用了Newtonsoft.Json的第三方dll。
至此代码完成,执行函数,成功
然后可以获取一下,或者到手机微信看一看,到微信手机看的时候要先取消关注,清空缓存,再关注就可以看到最新的了,如果还不行,多试几次。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23/// <summary> /// 拉取微信自定义菜单 /// </summary> /// <returns></returns> public string getMenu() { try { Hashtable hash = new Hashtable(); hash.Add("access_token", access_token()); string menu = Util.HttpRequestPostBody("https://api.weixin.qq.com/cgi-bin/menu/get", hash); Hashtable hr = JsonConvert.DeserializeObject<Hashtable>(menu); if (hr.Contains("errmsg")) { throw new Exception(hr["errmsg"].ToString()); } return menu; } catch (Exception x) { throw new Exception(x.Message); } }
最后
以上就是难过故事最近收集整理的关于微信公众号的开发之 自定义菜单(二)的全部内容,更多相关微信公众号的开发之内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复