微信平台自定义菜单代码
2014-05-08 13:37
337 查看
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Net;
public partial class wx_weixin : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// 首先根据微信的接口说明 获取你的 access_token 值
//然后 利用提供的文件直接上传运行,根据显示的返回 参考判断是否正确,如果返回的是 {"errcode":0,"errmsg":"ok"} 则成功。
//保证能用,有问题可以咨询
//所有的 key 和name 都是可以自己定义,结合公众平台文档,根据自己需要调整
string weixin1 = "";
weixin1 += "{\n";
weixin1 += "\"button\":[\n";
weixin1 += "{\n";
weixin1 += "\"type\":\"click\",\n";
weixin1 += "\"name\":\"公司简介\",\n";
weixin1 += "\"key\":\"jianjie\"\n";
weixin1 += "},\n";
weixin1 += "{\n";
weixin1 += "\"type\":\"click\",\n";
weixin1 += "\"name\":\"在线订房\",\n";
weixin1 += "\"key\":\"order\"\n";
weixin1 += "},\n";
weixin1 += "{\n";
weixin1 += "\"name\":\"我的菜单\",\n";
weixin1 += "\"sub_button\":[\n";
weixin1 += "{\n";
weixin1 += "\"type\":\"click\",\n";
weixin1 += "\"name\":\"子菜单1\",\n";
weixin1 += "\"key\":\"zcd1\"\n";
weixin1 += "},\n";
weixin1 += "{\n";
weixin1 += "\"type\":\"view\",\n";
weixin1 += "\"name\":\"子菜单2\",\n";
weixin1 += "\"key\":\"zcd2\"\n";
weixin1 += "}]\n";
weixin1 += "}]\n";
weixin1 += "}\n";
string i = GetPage("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=改成你自己的access_token",
weixin1);
Response.Write(i);
}
public string GetPage(string posturl, string postData)
{
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(posturl) as HttpWebRequest;
CookieContainer cookieContainer = new CookieContainer();
request.CookieContainer = cookieContainer;
request.AllowAutoRedirect = true;
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();
string err = string.Empty;
return content;
}
catch (Exception ex)
{
string err = ex.Message;
Response.Write(err);
return string.Empty;
}
}
}
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Net;
public partial class wx_weixin : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// 首先根据微信的接口说明 获取你的 access_token 值
//然后 利用提供的文件直接上传运行,根据显示的返回 参考判断是否正确,如果返回的是 {"errcode":0,"errmsg":"ok"} 则成功。
//保证能用,有问题可以咨询
//所有的 key 和name 都是可以自己定义,结合公众平台文档,根据自己需要调整
string weixin1 = "";
weixin1 += "{\n";
weixin1 += "\"button\":[\n";
weixin1 += "{\n";
weixin1 += "\"type\":\"click\",\n";
weixin1 += "\"name\":\"公司简介\",\n";
weixin1 += "\"key\":\"jianjie\"\n";
weixin1 += "},\n";
weixin1 += "{\n";
weixin1 += "\"type\":\"click\",\n";
weixin1 += "\"name\":\"在线订房\",\n";
weixin1 += "\"key\":\"order\"\n";
weixin1 += "},\n";
weixin1 += "{\n";
weixin1 += "\"name\":\"我的菜单\",\n";
weixin1 += "\"sub_button\":[\n";
weixin1 += "{\n";
weixin1 += "\"type\":\"click\",\n";
weixin1 += "\"name\":\"子菜单1\",\n";
weixin1 += "\"key\":\"zcd1\"\n";
weixin1 += "},\n";
weixin1 += "{\n";
weixin1 += "\"type\":\"view\",\n";
weixin1 += "\"name\":\"子菜单2\",\n";
weixin1 += "\"key\":\"zcd2\"\n";
weixin1 += "}]\n";
weixin1 += "}]\n";
weixin1 += "}\n";
string i = GetPage("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=改成你自己的access_token",
weixin1);
Response.Write(i);
}
public string GetPage(string posturl, string postData)
{
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(posturl) as HttpWebRequest;
CookieContainer cookieContainer = new CookieContainer();
request.CookieContainer = cookieContainer;
request.AllowAutoRedirect = true;
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();
string err = string.Empty;
return content;
}
catch (Exception ex)
{
string err = ex.Message;
Response.Write(err);
return string.Empty;
}
}
}
相关文章推荐
- 微信成为开发者模式
- 微信二维码登录原理
- 支付宝也要上"服务号"?斗战微信继续升级
- 微微信.NET 为什么采用文件系统而不是数据库?
- 微信公平台的开发之初
- 微信订阅号开发笔记(四)
- 微信订阅号开发笔记(三)
- 微信公众平台消息接口开发-封装weixin.class.php(转)
- 微信公众平台开发(数据库连接)
- 微信公众号开发总结
- 微信订阅号开发笔记(二)
- 解读微信质变、解读张小龙的七大价值观、解读领导艺术
- 仿乐享微信源码分享,把你的生意做到微信里
- 未来十年营销主流趋势:微信营销
- 做好微信营销方案的重要性
- 张小龙 微信事业群
- 万亿移动支付产业的难点和痛点
- 微信快速开发框架(七)--发送客服信息,版本更新至V2.2 代码已更新至github
- 史上最文艺自虐手游诞生-痛并快乐着
- 微信与腾讯国家队说拜拜