创建自定义菜单
1、 首先通过https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=appid&secret=secret获取access_token
2、 通过https://api.weixin.qq.com/cgi-bin/menu/create?access_token=access_token 创建菜单 代码如下
/// <summary>
/// 创建菜单
/// </summary>
public void CreateMenus()
{
//测试access_token
//string url = https://api.weixin.qq.com/cgi-bin/menu/create?access_token=access_token;
//正式access_token
string url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=access_token";
string param = "{\"button\":[{\"name\":\"操盘必读\",\"sub_button\":[{\"type\":\"click\",\"name\":\"财经要闻\",\"key\":\"cjyw\"},{\"type\":\"click\",\"name\":\"名家解盘\",\"key\":\"mjjp\"},{\"type\":\"click\",\"name\":\"财富早九点\",\"key\":\"cfzjd\"}]},";
param += "{\"type\":\"click\",\"name\":\"好股\",\"key\":\"hg\"},{\"name\":\"更多\",\"sub_button\":[{\"type\":\"click\",\"name\":\"个股体检\",\"key\":\"ggtj\"},{\"type\":\"click\",\"name\":\"帮助\",\"key\":\"bz\"}]}]}";
string result = PostWebReq(url, param, Encoding.UTF8);
LiteralMenu.Text = result;
}
public string PostWebReq(string postUrl,string paramData,Encoding dataEncode)
{
string ret = string.Empty;
try
{
byte[] byteArray = dataEncode.GetBytes(paramData);
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
webReq.Method = "POST";
webReq.ContentType = "application/x-www-form-urlencoded";
webReq.ContentLength = byteArray.Length;
Stream newStream = webReq.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length);
newStream.Close();
HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default);
ret = sr.ReadToEnd();
sr.Close();
response.Close();
newStream.Close();
}
catch (Exception)
{
}
return ret;
}