这篇文章主要介绍了asp.net微信开发中有关开发者接入的相关内容,需要的朋友可以参考下
先上图,看一看需要进行哪些项目的操作:
在项目的根目录或者特定的文件夹内,创建一个ashx文件(一般处理程序文件),如图
public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string postString = string.Empty; if (HttpContext.Current.Request.HttpMethod.ToUpper() == "POST") { using (Stream stream = HttpContext.Current.Request.InputStream) { Byte[] postBytes = new Byte[stream.Length]; stream.Read(postBytes, 0, (Int32)stream.Length); postString = Encoding.UTF8.GetString(postBytes); } if (!string.IsNullOrEmpty(postString)) { ResponseXML(postString);//返回给微信用户信息 } ///加载自定义菜单 string postData = "{" + " "; postData += ""button":[ " + " "; postData += "{ " + " "; postData += ""name":"简单查"," + " "; postData += ""sub_button":[" + " "; postData += "{ " + " "; postData += " "type":"click"," + " "; postData += " "name":"我的薪资", " + " "; postData += " "key":"mypay"" + " "; postData += "}," + " "; postData += "{ " + " "; postData += " "type":"click"," + " "; postData += " "name":"天气预报", " + " "; postData += " "key":"tianqiyubao"" + " "; postData += "}," + " "; postData += "{ " + " "; postData += " "type":"view"," + " "; postData += " "name":"火车票查询", " + " "; postData += " "url":"http://www.deqiaohr.com.cn/*******.aspx"" + " "; postData += "}," + " "; postData += "{ " + " "; postData += " "type":"click"," + " "; postData += " "name":"开心一刻", " + " "; postData += " "key":"kaixinyixiao"" + " "; postData += " }]" + " "; postData += "}," + " "; postData += "{" + " "; postData += ""name":"会员管理", " + " "; postData += ""sub_button":[" + " "; postData += "{ " + " "; postData += " "type":"view"," + " "; postData += " "name":"会员注册", " + " "; postData += " "url":"http://www.deqiaohr.com.cn/****.aspx"" + " "; postData += "}," + " "; postData += "{ " + " "; postData += " "type":"view"," + " "; postData += " "name":"重置密码", " + " "; postData += " "url":"http://www.deqiaohr.com.cn/****.aspx"" + " "; postData += "}," + " "; postData += "{ " + " "; postData += " "type":"click"," + " "; postData += " "name":"修改资料", " + " "; postData += " "key":"updateMessage"" + " "; postData += "}," + " "; postData += "{ " + " "; postData += " "type":"click"," + " "; postData += " "name":"我的提问", " + " "; postData += " "key":"mywen"" + " "; postData += "}," + " "; postData += "{ " + " "; postData += " "type":"click"," + " "; postData += " "name":"联系客服", " + " "; postData += " "key":"PhoneSerices"" + " "; postData += " }]" + " "; postData += "}," + " "; postData += "{" + " "; postData += ""name":"活动通知"," + " "; postData += ""sub_button":[" + " "; postData += "{ " + " "; postData += " "type":"click"," + " "; postData += " "name":"近期活动", " + " "; postData += " "key":"yuangonghuodong"" + " "; postData += "}," + " "; postData += "{ " + " "; postData += " "type":"click"," + " "; postData += " "name":"近期通知", " + " "; postData += " "key":"yuangongtongzhi"" + " "; postData += "}," + " "; postData += "{ " + " "; postData += " "type":"click"," + " "; postData += " "name":"有问必答", " + " "; postData += " "key":"youwenbida"" + " "; postData += " }]" + " "; postData += "}]" + " "; postData += "}" + " "; //自定义菜单token的获取 是用 下面的两个参数 获取的 不能直接用 公众平台的token string to = GetAccessToken(); //本人不喜欢 后台 json的操作 直接截取就可以了 得到的就是 token 或者 自己 获取 json的token to = to.Substring(17, to.Length - 37); //加载菜单 string i = GetPage("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + to, postData); } else { Auth(); //微信接入的测试 } } ///登录后复制/// 获取通行证 /// ///private string GetAccessToken() { string url_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=********&secret=*********"; HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url_token); myRequest.Method = "GET"; HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); string content = reader.ReadToEnd(); reader.Close(); return content; } /// /// 加载菜单项 /// /// /// ///private string GetPage(string p, 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(p) 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; } } /// /// 获取参数进行认证 /// private void Auth() { string token = "*****";//你申请的时候填写的Token string echoString = HttpContext.Current.Request.QueryString["echoStr"]; string signature = HttpContext.Current.Request.QueryString["signature"]; string timestamp = HttpContext.Current.Request.QueryString["timestamp"]; string nonce = HttpContext.Current.Request.QueryString["nonce"]; if (CheckSignature(token, signature, timestamp, nonce)) { if (!string.IsNullOrEmpty(echoString)) { HttpContext.Current.Response.Write(echoString); HttpContext.Current.Response.End(); } } } ////// 对微信传入参数进行封装到数组,拼接字符串,进行加密操作 /// /// /// /// /// ///private bool CheckSignature(string token, string signature, string timestamp, string nonce) { 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; } }
以上就是asp.net微信开发开发者接入技巧的详细内容,更多请关注慧达安全导航其它相关文章!
免责 声明
1、本网站名称:慧达安全导航
2、本站永久网址:https//www.huida178.com/
3、本站所有资源来源于网友投稿和高价购买,所有资源仅对编程人员及源代码爱好者开放下载做参考和研究及学习,本站不提供任何技术服务!
4、本站所有资源的属示图片和信息不代表本站的立场!本站只是储蓄平台及搬运
5、下载者禁止在服务器和虚拟机下进行搭建运营,本站所有资源不支持联网运行!只允许调试,参考和研究!!!!
6、未经原版权作者许可禁止用于任何商业环境,任何人不得擅作它用,下载者不得用于违反国家法律,否则发生的一切法律后果自行承担!
7、为尊重作者版权,请在下载24小时内删除!请购买原版授权作品,支持你喜欢的作者,谢谢!
8.若资源侵犯了您的合法权益,请持 您的版权证书和相关原作品信息来信通知我们!QQ:1247526623我们会及时删除,给您带来的不便,我们深表歉意!
9、如下载链接失效、广告或者压缩包问题请联系站长处理
10、如果你也有好源码或者教程,可以发布到网站,分享有金币奖励和额外收入!
11、本站资源售价只是赞助,收取费用仅维持本站的日常运营所需
12、因源码具有可复制性,一经赞助,不得以任何形式退款。
13、本文内容由网友自发贡献和站长收集,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系1247526623@qq.com
转载请注明出处: 慧达安全导航 » asp.net微信开发开发者接入技巧
发表评论 取消回复