asp.net微信开发之开发者接入技巧有哪些
发表于:2024-11-26 作者:千家信息网编辑
千家信息网最后更新 2024年11月26日,这篇文章主要介绍asp.net微信开发之开发者接入技巧有哪些,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!先上图,看一看需要进行哪些项目的操作:在项目的根目录或者特定的文件夹内
千家信息网最后更新 2024年11月26日asp.net微信开发之开发者接入技巧有哪些
这篇文章主要介绍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 = "{" + "\r\n"; postData += "\"button\":[ " + "\r\n"; postData += "{ " + "\r\n"; postData += "\"name\":\"简单查\"," + "\r\n"; postData += "\"sub_button\":[" + "\r\n"; postData += "{ " + "\r\n"; postData += " \"type\":\"click\"," + "\r\n"; postData += " \"name\":\"我的薪资\", " + "\r\n"; postData += " \"key\":\"mypay\"" + "\r\n"; postData += "}," + "\r\n"; postData += "{ " + "\r\n"; postData += " \"type\":\"click\"," + "\r\n"; postData += " \"name\":\"天气预报\", " + "\r\n"; postData += " \"key\":\"tianqiyubao\"" + "\r\n"; postData += "}," + "\r\n"; postData += "{ " + "\r\n"; postData += " \"type\":\"view\"," + "\r\n"; postData += " \"name\":\"火车票查询\", " + "\r\n"; postData += " \"url\":\"http://www.deqiaohr.com.cn/*******.aspx\"" + "\r\n"; postData += "}," + "\r\n"; postData += "{ " + "\r\n"; postData += " \"type\":\"click\"," + "\r\n"; postData += " \"name\":\"开心一刻\", " + "\r\n"; postData += " \"key\":\"kaixinyixiao\"" + "\r\n"; postData += " }]" + "\r\n"; postData += "}," + "\r\n"; postData += "{" + "\r\n"; postData += "\"name\":\"会员管理\", " + "\r\n"; postData += "\"sub_button\":[" + "\r\n"; postData += "{ " + "\r\n"; postData += " \"type\":\"view\"," + "\r\n"; postData += " \"name\":\"会员注册\", " + "\r\n"; postData += " \"url\":\"http://www.deqiaohr.com.cn/****.aspx\"" + "\r\n"; postData += "}," + "\r\n"; postData += "{ " + "\r\n"; postData += " \"type\":\"view\"," + "\r\n"; postData += " \"name\":\"重置密码\", " + "\r\n"; postData += " \"url\":\"http://www.deqiaohr.com.cn/****.aspx\"" + "\r\n"; postData += "}," + "\r\n"; postData += "{ " + "\r\n"; postData += " \"type\":\"click\"," + "\r\n"; postData += " \"name\":\"修改资料\", " + "\r\n"; postData += " \"key\":\"updateMessage\"" + "\r\n"; postData += "}," + "\r\n"; postData += "{ " + "\r\n"; postData += " \"type\":\"click\"," + "\r\n"; postData += " \"name\":\"我的提问\", " + "\r\n"; postData += " \"key\":\"mywen\"" + "\r\n"; postData += "}," + "\r\n"; postData += "{ " + "\r\n"; postData += " \"type\":\"click\"," + "\r\n"; postData += " \"name\":\"联系客服\", " + "\r\n"; postData += " \"key\":\"PhoneSerices\"" + "\r\n"; postData += " }]" + "\r\n"; postData += "}," + "\r\n"; postData += "{" + "\r\n"; postData += "\"name\":\"活动通知\"," + "\r\n"; postData += "\"sub_button\":[" + "\r\n"; postData += "{ " + "\r\n"; postData += " \"type\":\"click\"," + "\r\n"; postData += " \"name\":\"近期活动\", " + "\r\n"; postData += " \"key\":\"yuangonghuodong\"" + "\r\n"; postData += "}," + "\r\n"; postData += "{ " + "\r\n"; postData += " \"type\":\"click\"," + "\r\n"; postData += " \"name\":\"近期通知\", " + "\r\n"; postData += " \"key\":\"yuangongtongzhi\"" + "\r\n"; postData += "}," + "\r\n"; postData += "{ " + "\r\n"; postData += " \"type\":\"click\"," + "\r\n"; postData += " \"name\":\"有问必答\", " + "\r\n"; postData += " \"key\":\"youwenbida\"" + "\r\n"; postData += " }]" + "\r\n"; postData += "}]" + "\r\n"; postData += "}" + "\r\n"; //自定义菜单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微信开发之开发者接入技巧有哪些"这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!
参数
开发
数组
菜单
接入
文件
开发者
技巧
会员
内容
字符
字符串
程序
篇文章
网页
项目
加密
活动
有问必答
一刻
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
梦幻诛仙服务器哪个好
员工离职 软件开发
大学生网络安全知识教育论文
联想台式服务器设置u盘启动
csgo顶级皮肤服务器
东营高校党建软件开发专业制作
我国网络安全事件应急响应
网络安全女警画法
linux虚拟服务器
绍兴嵌入式软件开发计划
北流市网络安全大队
cobol项目数据库
178战舰世界数据库
邯郸程序软件开发大概要多少钱
魔兽世界怀旧部落去哪个服务器
app软件开发外包服务
未来电脑网络技术
python怎么做数据库界面
mysql简单数据库
关系数据库文件中的记录
计算机三级网络技术电子课本
计算机网络技术可专升本吗
网络安全弊
深圳路通网络技术怎么样
济南德良网络技术
网络安全适合女孩学吗
天下379服务器
华工 数据库应用 考试
网络安全法在国内建设运营
哪里的网络安全公司比较强