protected void ButtonTwo_Click(object sender, EventArgs e) { if (!CheckAppSetting()) return; txtMenuString.Text = GetMenu(); SaveConfig(); if (String.IsNullOrEmpty(txtMenuString.Text.Trim())) { ShowLog("请填写菜单信息"); return; } // if (MessageBox.Show("您确定要创建或更新菜单吗?", "确定更新", MessageBoxButtons.YesNo) != DialogResult.Yes) return; //1:获得token MyHttpWebRequest request = new MyHttpWebRequest(); string token = GetToken(); if (String.IsNullOrEmpty(token)) { ShowLog("获取Token失败!"); return; } //2:删除当前菜单 string rtn = request.getPage("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" + token); ShowLog("清除菜单返回:\r\n" + rtn); //3:创建菜单 NameValueCollection nvc = new NameValueCollection(); nvc.Add("", txtMenuString.Text); rtn = request.post("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + token, nvc, Encoding.UTF8); ShowLog("提交菜单返回:" + rtn); }
protected void ButtonOne_Click(object sender, EventArgs e) { if (!CheckAppSetting()) return; // if (MessageBox.Show("您确定要获菜单数据吗?获取后将会覆盖现在的菜单结构体内容!", "确定更新", MessageBoxButtons.YesNo) != DialogResult.Yes) return; MyHttpWebRequest request = new MyHttpWebRequest(); string token = this.GetToken(); if (String.IsNullOrEmpty(token)) { ShowLog("获取Token失败!"); return; } string rtn = request.getPage(String.Format("https://api.weixin.qq.com/cgi-bin/menu/get?access_token={0}", token)); try { LitJson.JsonData data = LitJson.JsonMapper.ToObject(rtn); if (!String.IsNullOrEmpty(data["errcode"].ToString())) { ShowLog("获取菜单返回:\r\n" + rtn); } else { txtMenuString.Text = rtn; } } catch (Exception ex) { txtMenuString.Text = rtn; } }
protected void ButtonThree_Click(object sender, EventArgs e) { if (!CheckAppSetting()) return; // if (MessageBox.Show("您确定清除微信菜单吗?清除后您还可以重建菜单!", "确定清除", MessageBoxButtons.YesNo) != DialogResult.Yes) return; txtMenuString.Text = GetMenu(); SaveConfig(); MyHttpWebRequest request = new MyHttpWebRequest(); string token = this.GetToken(); if (String.IsNullOrEmpty(token)) { ShowLog("获取Token失败!"); return; } string rtn = request.getPage("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" + token); ShowLog("清除菜单返回:\r\n" + rtn); }
/// <summary> /// 获取Token /// </summary> /// <returns></returns> private string GetToken() { if (!String.IsNullOrEmpty(Token)) { if (DateTime.Now.Subtract(TokenTime).TotalSeconds < expires) return Token; } MyHttpWebRequest request = new MyHttpWebRequest(); string rtn = request.getPage(String.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", txtAppid.Text, txtKey.Text)); ShowLog("获取Token返回:" + rtn); try { LitJson.JsonData data = LitJson.JsonMapper.ToObject(rtn); Token = data["access_token"].ToString(); TokenTime = DateTime.Now; expires = Convert.ToInt32(data["expires_in"].ToString()); return Token; } catch (Exception ex) { ShowLog(ex.Message); } return ""; }