You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

615 lines
32 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
namespace TradeManage.WeiXin
{
public partial class weixin_api : System.Web.UI.Page
{
const string Token = "qianyun"; //你的token
static string appId = "wx2f5849d8aa573da3";//公众号的appId
static string appSecret = "34cc0ed309a8250eed74ea4df0b59fb5";//公众号的appSecret
static string appUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";
static string postUrl = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=";
static string postDelUrl = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string postStr = "";
if (Request.HttpMethod.ToUpper() == "GET")
{
Valid();
}
else
if (Request.HttpMethod.ToUpper() == "POST")
{
Stream s = System.Web.HttpContext.Current.Request.InputStream;
byte[] b = new byte[s.Length];
s.Read(b, 0, (int)s.Length);
postStr = Encoding.UTF8.GetString(b);
if (!string.IsNullOrEmpty(postStr))
{
ResponseMsg(postStr);
}
//WriteLog("postStr:" + postStr);
}
}
}
/// <summary>
/// 验证微信签名
/// </summary>
/// * 将token、timestamp、nonce三个参数进行字典序排序
/// * 将三个参数字符串拼接成一个字符串进行sha1加密
/// * 开发者获得加密后的字符串可与signature对比标识该请求来源于微信。
/// <returns></returns>
private bool CheckSignature()
{
string signature = "";
string timestamp = "";
string nonce = "";
if ((Server.UrlDecode(Page.Request.QueryString["signature"]) != null) && (Server.UrlDecode(Page.Request.QueryString["signature"]) != ""))
{
signature = Request.QueryString["signature"].ToString();
timestamp = Request.QueryString["timestamp"].ToString();
nonce = Request.QueryString["nonce"].ToString();
}
string[] ArrTmp = { Token, timestamp, nonce };
Array.Sort(ArrTmp); //字典排序
string tmpStr = string.Join("", ArrTmp);
tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");
tmpStr = tmpStr.ToLower();
if (tmpStr == signature)
{
return true;
}
else
{
return false;
}
}
private void Valid()
{
string echoStr = "";
if ((Server.UrlDecode(Page.Request.QueryString["echoStr"]) != null) && (Server.UrlDecode(Page.Request.QueryString["echoStr"]) != ""))
{
echoStr = Request.QueryString["echoStr"].ToString();
}
if (CheckSignature())
{
if (!string.IsNullOrEmpty(echoStr))
{
Response.Write(echoStr);
Response.End();
}
}
}
/// <summary>
/// 将xml文件转换成Hashtable
/// </summary>
/// <param name="xml"></param>
/// <returns></returns>
public static Hashtable ParseXml(String xml)
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xml);
XmlNode bodyNode = xmlDocument.ChildNodes[0];
Hashtable ht = new Hashtable();
if (bodyNode.ChildNodes.Count > 0)
{
foreach (XmlNode xn in bodyNode.ChildNodes)
{
ht.Add(xn.Name, xn.InnerText);
}
}
return ht;
}
/// <summary>
/// 返回信息结果(微信信息返回)
/// </summary>
/// <param name="weixinXML"></param>
private void ResponseMsg(string weixinXML)
{
try
{
//ErrorFollow.TraceWrite("微信接收", "", weixinXML);
XmlDocument doc = new XmlDocument();
doc.LoadXml(weixinXML);//读取XML字符串
XmlElement rootElement = doc.DocumentElement;
XmlNode MsgType = rootElement.SelectSingleNode("MsgType");//获取字符串中的消息类型
string resxml = "";
//var model = new
//{
// ToUserName = rootElement.SelectSingleNode("ToUserName").InnerText,
// FromUserName = rootElement.SelectSingleNode("FromUserName").InnerText,
// CreateTime = rootElement.SelectSingleNode("CreateTime").InnerText,
// MsgType = MsgType.InnerText,
// PicUrl = rootElement.SelectSingleNode("PicUrl").InnerText,
// MsgId = rootElement.SelectSingleNode("MsgId").InnerText
//};
//resxml += "<xml><ToUserName><![CDATA[" + model.FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + model.ToUserName + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt(DateTime.Now) + "</CreateTime><MsgType><![CDATA[news]]></MsgType><ArticleCount>1</ArticleCount><Articles><item><Title><![CDATA[欢迎您来到云界健康馆!]]></Title><Description><![CDATA[赶快进去看看吧!]]></Description><PicUrl><![CDATA[http://121.43.228.63/images/wxlj.png]]></PicUrl><Url><![CDATA[http://weidian.com/?userid=318755832&wfr=wechatpo_keywords_shop]]></Url></item></Articles><FuncFlag>1</FuncFlag></xml>";
//Response.Write(resxml);
//return;
if (MsgType.InnerText == "text")//如果消息类型为文本消息
{
var model = new
{
ToUserName = rootElement.SelectSingleNode("ToUserName").InnerText,
FromUserName = rootElement.SelectSingleNode("FromUserName").InnerText,
CreateTime = rootElement.SelectSingleNode("CreateTime").InnerText,
MsgType = MsgType.InnerText,
Content = rootElement.SelectSingleNode("Content").InnerText,
MsgId = rootElement.SelectSingleNode("MsgId").InnerText
};
//resxml += "<xml><ToUserName><![CDATA[" + model.FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + model.ToUserName + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt(DateTime.Now) + "</CreateTime>";
resxml += "<xml><ToUserName><![CDATA[" + model.FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + model.ToUserName + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt(DateTime.Now) + "</CreateTime><MsgType><![CDATA[news]]></MsgType><ArticleCount>1</ArticleCount><Articles><item><Title><![CDATA[欢迎您来到云界健康馆!]]></Title><Description><![CDATA[赶快进去看看吧!]]></Description><PicUrl><![CDATA[http://121.43.228.63/images/wxlj.png]]></PicUrl><Url><![CDATA[http://weidian.com/?userid=318755832&wfr=wechatpo_keywords_shop]]></Url></item></Articles><FuncFlag>1</FuncFlag></xml>";
if (!string.IsNullOrEmpty(model.Content))//如果接收到消息
{
string xz = "";
//if (model.Content.Contains("摩羯"))//
//{
// xz = "2014年由于受到木星与天王星良好相位的影响摩羯座在各方面的表现都称心如意。但由于土星在前半年一直对冲摩羯座摩羯们在心情方面可能会感到有些压抑以致思想和行动力上都有些保守、退缩不前有时还会特别固执...";
//}
//else if (model.Content.Contains("金牛"))//
//{
// xz = "2014年整体而言财富有赚有赔、有高成长也有大支出所幸在收支相抵后仍有盈余。不少人兴起换屋、买车、添购电脑、大型家电用品或奢侈品的冲动花钱请客较平日慷慨大方投资运明显欠佳不利投机或风险过高的投资活动...";
//}
//else if (model.Content.Contains("双子"))//
//{
// xz = "双子座对事业充满了理想,也能投入在负责的事务之中,各个方面都能够运作得很顺利。对工作认真积极的态度,为双子们带来不错的绩效,而且实质的回收也很可观。若接受友人的建议和帮助...";
//}
//else if (model.Content.Contains("巨蟹"))//
//{
// xz = "巨蟹们在个人心态上会有些保守但不顽固。而天王星的加入会使巨蟹座渴望尝试一些新的东西但又害怕改变原有的生活模式。在今年巨蟹打工族们在工作上的表现还算不错是非常忙碌的一年。在7、8、9月巨蟹们要特别小心别出差错...";
//}
//else if (model.Content.Contains("狮子"))//
//{
// xz = "于狮子座自我的成就感不足造成疲惫与受挫对於原本的工作内容与职务开始思考与反省。到了2014年下半年狮子座的朋友的事业运有所好转可能因为工作环境改变或者是更换跑道从而找到最适合自己的方向...";
//}
//else if (model.Content.Contains("处女"))//
//{
// xz = "2014年情运对处女座来说变化多端的一年要鼓起勇气去面对一连串的变化似乎是冥冥中的缘分促成一些奇遇工作上的场合或是跟新旧朋友的相聚而使感情进展颇为快速能够陶醉其中...";
//}
//else if (model.Content.Contains("天秤"))//
//{
// xz = "天秤座2014年恋爱运不是很平稳由于恋爱宫位受到火星和天王星的干扰可能有一些小风波需要用心去面对来化解问题。有伴侣的天秤座大约八九月的年中或许会出现一个考验和危机不过只要彼此都有诚心问题应该不难解决...";
//}
//else if (model.Content.Contains("天蝎"))//
//{
// xz = "2014年天蝎座的运势起伏不断历经周遭环境的动荡然而天蝎座的适应力会适时发挥展现出极强的生命力。后半年天蝎座的运势会开始好转并逐渐旺盛起来。前后形成了强烈的对比不过都同样都是以强韧生命力展现贯穿了这一整年...";
//}
//else if (model.Content.Contains("射手"))//
//{
// xz = "2014年射手座的感情变动很大由於你的恋爱宫位受黯月(月孛)投射的影响,与月轨交点同时产生感应,因而产生层面广泛的宿命作用力。射手会发觉感情事件的际遇和演变,似乎是早已注定的...";
//}
//else if (model.Content.Contains("水瓶") || model.Content.Contains("水平"))//
//{
// xz = "2014年海王星将一直坐守于水瓶座水瓶们在情绪上会显得有些敏感 、忧虑,行事之前想法过于周密,甚至对于感情也陷入一种暧昧不清的状态。 此外,今年的木星也一直在给予水瓶们正面的影响,虽然经经常会有不顺利的时候, 但最终达成目标...";
//}
//else if (model.Content.Contains("双鱼"))//
//{
// xz = "双鱼座2014年受爱神星的笼罩感情运会非常好特别是在五月之後更有绝佳的好运!有伴侣的你,与对方的感情会越来越深,有机会踏上新的阶段。如果你们原本就非常不合适,有可能会换新的伴侣......";
//}
//else if (model.Content.Contains("白羊"))//
//{
// xz = "精力旺盛、办事能力强的白羊座在2014年事业运较好。所谓种瓜得瓜种豆得豆一份耕耘一份收获。辛苦的付出终会有回报努力拼博的你不仅各种业务都能应付自如还能走出一片新的天空。对于工薪阶层7月份将有机会获得提升...";
//}
//else
//{
// xz = "你好,欢迎来到汇众视界,我们是一家专门从事软硬件开发,系统集成的高新技术公司,当前这个服务号还处于测试阶段,更多功能近请期待";
// }
//resxml += "<MsgType><![CDATA[text]]></MsgType><Content><![CDATA[" + xz + "]]></Content><FuncFlag>0</FuncFlag></xml>";
}
//else//没有接收到消息
//{
// resxml += "<MsgType><![CDATA[text]]></MsgType><Content><![CDATA[你好,欢迎来到汇众视界,我们是一家专门从事软硬件开发,系统集成的高新技术公司,当前这个服务号还处于测试阶段,更多功能近请期待]]></Content><FuncFlag>0</FuncFlag></xml>";
//}
// ErrorFollow.TraceWrite("微信回复", "", resxml);
Response.Write(resxml);
}
else if (MsgType.InnerText == "image")//如果消息类型为图片消息
{
var model = new
{
ToUserName = rootElement.SelectSingleNode("ToUserName").InnerText,
FromUserName = rootElement.SelectSingleNode("FromUserName").InnerText,
CreateTime = rootElement.SelectSingleNode("CreateTime").InnerText,
MsgType = MsgType.InnerText,
PicUrl = rootElement.SelectSingleNode("PicUrl").InnerText,
MsgId = rootElement.SelectSingleNode("MsgId").InnerText
};
//resxml += "<xml><ToUserName><![CDATA[" + model.FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + model.ToUserName + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt(DateTime.Now) + "</CreateTime><MsgType><![CDATA[news]]></MsgType><ArticleCount>1</ArticleCount><Articles><item><Title><![CDATA[欢迎您来到汇众科技!]]></Title><Description><![CDATA[非常感谢您的关注!]]></Description><PicUrl><![CDATA[http://122.227.245.29/images/1rj2.jpg]]></PicUrl><Url><![CDATA[http://122.227.245.29/login.aspx]]></Url></item></Articles><FuncFlag>1</FuncFlag></xml>";
resxml += "<xml><ToUserName><![CDATA[" + model.FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + model.ToUserName + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt(DateTime.Now) + "</CreateTime><MsgType><![CDATA[news]]></MsgType><ArticleCount>1</ArticleCount><Articles><item><Title><![CDATA[欢迎您来到云界健康馆!]]></Title><Description><![CDATA[赶快进去看看吧!]]></Description><PicUrl><![CDATA[http://121.43.228.63/images/wxlj.png]]></PicUrl><Url><![CDATA[http://weidian.com/?userid=318755832&wfr=wechatpo_keywords_shop]]></Url></item></Articles><FuncFlag>1</FuncFlag></xml>";
Response.Write(resxml);
}
else
if (MsgType.InnerText == "event")//如果消息类型为图片消息
{
var model = new
{
ToUserName = rootElement.SelectSingleNode("ToUserName").InnerText,
FromUserName = rootElement.SelectSingleNode("FromUserName").InnerText,
CreateTime = rootElement.SelectSingleNode("CreateTime").InnerText,
MsgType = MsgType.InnerText,
EventKey = rootElement.SelectSingleNode("EventKey").InnerText,//用于自定义菜单
Event = rootElement.SelectSingleNode("Event").InnerText
// MsgId = rootElement.SelectSingleNode("MsgId").InnerText
};
// resxml = "<xml><ToUserName><![CDATA[" + model.FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + model.ToUserName + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt(DateTime.Now) + "</CreateTime><MsgType><![CDATA[news]]></MsgType><ArticleCount>1</ArticleCount><Articles><item><Title><![CDATA[欢迎您来到云界健康馆!]]></Title><Description><![CDATA[赶快进去看看吧!]]></Description><PicUrl><![CDATA[http://121.43.228.63/images/wxlj.png]]></PicUrl><Url><![CDATA[http://weidian.com/?userid=318755832&wfr=wechatpo_keywords_shop]]></Url></item></Articles><FuncFlag>1</FuncFlag></xml>";
// Response.Write(resxml);
resxml = "<xml><ToUserName><![CDATA[" + model.FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + model.ToUserName + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt(DateTime.Now) + "</CreateTime>";
//if (model.EventKey.ToLower() == "v1001_hb")
//{
//}
//else
//{
string xz = "终于等到你!送个红包当见面礼!猛戳下方菜单栏-最新活动-点击“新人红包”即可领取现金红包奥~持续关注乾云健康生活馆,享更多优惠!";
resxml += "<MsgType><![CDATA[text]]></MsgType><Content><![CDATA[" + xz + "]]></Content><FuncFlag>0</FuncFlag></xml>";
//}
Response.Write(resxml);
}
else//如果是其余的消息类型
{
var model = new
{
ToUserName = rootElement.SelectSingleNode("ToUserName").InnerText,
FromUserName = rootElement.SelectSingleNode("FromUserName").InnerText,
CreateTime = rootElement.SelectSingleNode("CreateTime").InnerText,
};
resxml += "<xml><ToUserName><![CDATA[" + model.FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + model.ToUserName + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt(DateTime.Now) + "</CreateTime><MsgType><![CDATA[news]]></MsgType><ArticleCount>1</ArticleCount><Articles><item><Title><![CDATA[欢迎您来到云界健康馆!]]></Title><Description><![CDATA[赶快进去看看吧!]]></Description><PicUrl><![CDATA[http://121.43.228.63/images/wxlj.png]]></PicUrl><Url><![CDATA[http://weidian.com/?userid=318755832&wfr=wechatpo_keywords_shop]]></Url></item></Articles><FuncFlag>1</FuncFlag></xml>";
//resxml += "<xml><ToUserName><![CDATA[" + model.FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + model.ToUserName + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt(DateTime.Now) + "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[你好,欢迎来到汇众视界,我们是一家专门从事软硬件开发,系统集成的高新技术公司,当前这个服务号还处于测试阶段,更多功能近请期待]]></Content><FuncFlag>0</FuncFlag></xml>";
Response.Write(resxml);
}
}
catch (Exception ex)
{
throw ex;
}
Response.End();
}
/// <summary>
/// unix时间转换为datetime
/// </summary>
/// <param name="timeStamp"></param>
/// <returns></returns>
private DateTime UnixTimeToTime(string timeStamp)
{
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
long lTime = long.Parse(timeStamp + "0000000");
TimeSpan toNow = new TimeSpan(lTime);
return dtStart.Add(toNow);
}
/// <summary>
/// datetime转换为unixtime
/// </summary>
/// <param name="time"></param>
/// <returns></returns>
private int ConvertDateTimeInt(System.DateTime time)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
return (int)(time - startTime).TotalSeconds;
}
/// <summary>
/// 写日志(用于跟踪)
/// </summary>
private void WriteLog(string strMemo)
{
string filename = Server.MapPath("/logs/log.txt");
if (!Directory.Exists(Server.MapPath("//logs//")))
Directory.CreateDirectory("//logs//");
StreamWriter sr = null;
try
{
if (!File.Exists(filename))
{
sr = File.CreateText(filename);
}
else
{
sr = File.AppendText(filename);
}
sr.WriteLine(strMemo);
}
catch
{
}
finally
{
if (sr != null)
sr.Close();
}
}
/// <summary>
/// 获取自定义菜单token
/// </summary>
/// <returns></returns>
public static string GetAccessToken()
{
WebClient webClient = new WebClient();
Byte[] bytes = webClient.DownloadData(string.Format("{0}&appid={1}&secret={2}", appUrl, appId, appSecret));
string result = Encoding.GetEncoding("utf-8").GetString(bytes);
//JObject jObj = JObject.Parse(result);
//JToken token = jObj["access_token"];
//return token.ToString().Substring(1, token.ToString().Length - 2);
string[] temp = result.Split(',');
string[] tp = temp[0].Split(':');
return tp[1].ToString().Replace('"', ' ').Trim().ToString();
}
/// <summary>
/// 创建自定义菜单
/// </summary>
private void CreateWxMenu()
{
string weixin1 = "";
weixin1 += "{\n";
weixin1 += "\"button\":[\n";
weixin1 += "{\n";
// weixin1 += "\"type\":\"click\",\n";
//第一个菜单
weixin1 += "\"name\":\"汇众科技\",\n";
weixin1 += "\"sub_button\":[\n";
weixin1 += "{\n";
weixin1 += "\"type\":\"click\",\n";
weixin1 += "\"name\":\"公司介绍\",\n";
weixin1 += "\"key\":\"V1001_About\"\n";
weixin1 += "},\n";
weixin1 += "{\n";
weixin1 += "\"type\":\"click\",\n";
weixin1 += "\"name\":\"组织结构\",\n";
weixin1 += "\"key\":\"V1001_Dept\"\n";
weixin1 += "}]\n";
weixin1 += "},\n";
//第二个菜单
weixin1 += "{\n";
//weixin1 += "\"type\":\"click\",\n";
weixin1 += "\"name\":\"产品目录\",\n";
weixin1 += "\"sub_button\":[\n";
weixin1 += "{\n";
weixin1 += "\"type\":\"click\",\n";
weixin1 += "\"name\":\"智慧医疗\",\n";
weixin1 += "\"key\":\"V1002_Hospital\"\n";
weixin1 += "},\n";
weixin1 += "{\n";
weixin1 += "\"type\":\"click\",\n";
weixin1 += "\"name\":\"教育信息化\",\n";
weixin1 += "\"key\":\"V1002_School\"\n";
weixin1 += "},\n";
weixin1 += "{\n";
weixin1 += "\"type\":\"click\",\n";
weixin1 += "\"name\":\"汇众视界\",\n";
weixin1 += "\"key\":\"V1002_View\"\n";
weixin1 += "}]\n";
weixin1 += "},\n";
//第三个菜单
weixin1 += "{\n";
weixin1 += "\"name\":\"合作加盟\",\n";
weixin1 += "\"sub_button\":[\n";
weixin1 += "{\n";
weixin1 += "\"type\":\"click\",\n";
weixin1 += "\"name\":\"人才招聘\",\n";
weixin1 += "\"key\":\"V1003_Rczp\"\n";
weixin1 += "},\n";
weixin1 += "{\n";
weixin1 += "\"type\":\"click\",\n";
weixin1 += "\"name\":\"合作共赢\",\n";
weixin1 += "\"key\":\"V1003_Hzgy\"\n";
weixin1 += "}]\n";
weixin1 += "}\n";
weixin1 += "]\n";
weixin1 += "}\n";
PostMenuData(postUrl + GetAccessToken(), weixin1);
}
private void CreateWxMenu2()
{
string weixin1 = "";
weixin1 += "{\n";
weixin1 += "\"button\":[\n";
weixin1 += "{\n";
// weixin1 += "\"type\":\"click\",\n";
//第一个菜单
weixin1 += "\"name\":\"最新活动\",\n";
weixin1 += "\"sub_button\":[\n";
weixin1 += "{\n";
weixin1 += "\"type\":\"view\",\n";
weixin1 += "\"name\":\"新人红包\",\n";
weixin1 += "\"url\":\"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx2f5849d8aa573da3&redirect_uri=http%3a%2f%2fwww.mntkj.com%2foauth2.aspx&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect\"\n";
weixin1 += "},\n";
weixin1 += "{\n";
weixin1 += "\"type\":\"view\",\n";
weixin1 += "\"name\":\"热卖商品\",\n";
weixin1 += "\"url\":\"http://weidian.com/?userid=318755832&wfr=wechatpo_keywords_shop\"\n";
weixin1 += "}]\n";
weixin1 += "},\n";
//第二个菜单
weixin1 += "{\n";
//weixin1 += "\"type\":\"click\",\n";
weixin1 += "\"name\":\"云界儿童\",\n";
weixin1 += "\"sub_button\":[\n";
weixin1 += "{\n";
weixin1 += "\"type\":\"view\",\n";
weixin1 += "\"name\":\"云界贝比亲\",\n";
weixin1 += "\"url\":\"http://weidian.com/item.html?itemID=1336709846\"\n";
weixin1 += "},\n";
weixin1 += "{\n";
weixin1 += "\"type\":\"view\",\n";
weixin1 += "\"name\":\"云界儿童宝\",\n";
weixin1 += "\"url\":\"http://weidian.com/item.html?itemID=1312408696\"\n";
weixin1 += "}]\n";
weixin1 += "},\n";
//第三个菜单
weixin1 += "{\n";
//weixin1 += "\"type\":\"click\",\n";
weixin1 += "\"name\":\"云界护理\",\n";
weixin1 += "\"sub_button\":[\n";
weixin1 += "{\n";
weixin1 += "\"type\":\"view\",\n";
weixin1 += "\"name\":\"云界护爱宝\",\n";
weixin1 += "\"url\":\"http://weidian.com/item.html?itemID=1338347234\"\n";
weixin1 += "},\n";
weixin1 += "{\n";
weixin1 += "\"type\":\"view\",\n";
weixin1 += "\"name\":\"云界快好\",\n";
weixin1 += "\"url\":\"http://weidian.com/item.html?itemID=1338342667\"\n";
weixin1 += "},\n";
weixin1 += "{\n";
weixin1 += "\"type\":\"view\",\n";
weixin1 += "\"name\":\"云界旅行宝\",\n";
weixin1 += "\"url\":\"http://weidian.com/item.html?itemID=1338359027\"\n";
weixin1 += "},\n";
weixin1 += "{\n";
weixin1 += "\"type\":\"view\",\n";
weixin1 += "\"name\":\"云界内衣净\",\n";
weixin1 += "\"url\":\"http://weidian.com/item.html?itemID=1338368589\"\n";
weixin1 += "},\n";
weixin1 += "{\n";
weixin1 += "\"type\":\"view\",\n";
weixin1 += "\"name\":\"云界抑菌剂\",\n";
weixin1 += "\"url\":\"http://weidian.com/item.html?itemID=1338355466\"\n";
weixin1 += "}]\n";
weixin1 += "}\n";
weixin1 += "]\n";
weixin1 += "}\n";
PostMenuData(postUrl + GetAccessToken(), weixin1);
}
/// <summary>
///
/// </summary>
/// <param name="url"></param>
/// <param name="postData"></param>
private void PostMenuData(string url, 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(url) 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;
//return string.Empty;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
CreateWxMenu();
}
/// <summary>
/// 删除自定义菜单
/// </summary>
/// <param name="url"></param>
private void DelMenu(string url)
{
if (string.IsNullOrEmpty(url))
{
throw new ArgumentNullException("url");
}
Encoding encoding = Encoding.UTF8;
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "GET";
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
Stream instream = response.GetResponseStream();
StreamReader sr = new StreamReader(instream, encoding);
string content = sr.ReadToEnd();
}
protected void Button2_Click(object sender, EventArgs e)
{
DelMenu(postDelUrl + GetAccessToken());
}
protected void Button1_Click1(object sender, EventArgs e)
{
CreateWxMenu2();
}
protected void Button2_Click1(object sender, EventArgs e)
{
DelMenu(postDelUrl + GetAccessToken());
}
}
}