您的位置:首页 > 移动开发 > 微信开发

微信公众号的开发之 自定义菜单(二)

2016-02-24 20:25 627 查看
首先,个人订阅号没有自定义菜单的权限,这时候可以使用测试公众号

步骤:定义json文件

{
"button": [
{
"type": "click",
"name": "菜单一",
"key": "one"
},
{
"type": "click",
"name": "菜单二",
"key": "two"
},
{
"type": "click",
"name": "菜单三",
"key": "three"

}
]
}
具体的json字段含义可以看http://mp.weixin.qq.com/wiki/10/0234e39a2025342c17a7d23595c6b40a.html

然后josn数据post到微信服务器

/// <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>
add6
(result)使用了Newtonsoft.Json的第三方dll。

至此代码完成,执行函数,成功

然后可以获取一下,或者到手机微信看一看,到微信手机看的时候要先取消关注,清空缓存,再关注就可以看到最新的了,如果还不行,多试几次。

/// <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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: