1 private void customMenu() 2 { 3 4 //获取access_token 5 string access_token = GetAccessToken(); 6 StringBuilder sb = new StringBuilder(); 7 sb.Append("{"button":[{"type":"click","name":"今日歌曲","key":"V1001_TODAY_MUSIC"},{"name":"菜单","sub_button":[{"type":"view","name":"搜索","url":"http://www.soso.com/"},{"type":"view","name":"视频","url":"http://v.qq.com/"},{"type":"click","name":"赞一下我们","key":"V1001_GOOD"}]}]}"); 8 HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + access_token); 9 byte[] requestBytes = Encoding.UTF8.GetBytes(sb.ToString()); 10 request.Method = "POST"; 11 request.ContentType = "application/x-www-form-urlencoded"; 12 request.ContentLength = requestBytes.Length; 13 Stream requestStream = request.GetRequestStream(); 14 requestStream.Write(requestBytes, 0, requestBytes.Length); 15 requestStream.Close(); 16 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 17 StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default); 18 string backstr = sr.ReadToEnd(); 19 sr.Close(); 20 response.Close(); 21 } 22 23 private static string GetAccessToken() 24 { 25 WebClient webClient = new WebClient(); 26 Byte[] bytes = webClient.DownloadData("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx44bc0f54a52d905d&secret=2328cba30d97f8ec3e613f7764a604a9"); 27 string token = Encoding.UTF8.GetString(bytes); 28 string[] result = token.Split(','); 29 string access_token1 = result[0].Split(':')[1]; 30 string access_token= access_token1.Substring(1,access_token1.Length-2); 31 return access_token; 32 }