当前位置:Gxlcms > asp.net > asp.net微信开发(开发者接入)

asp.net微信开发(开发者接入)

时间:2021-07-01 10:21:17 帮助过:18人阅读

先上图,看一看需要进行哪些项目的操作:

 

在项目的根目录或者特定的文件夹内,创建一个ashx文件(一般处理程序文件),如图

  1. public void ProcessRequest(HttpContext context)
  2. {
  3. context.Response.ContentType = "text/plain";
  4. string postString = string.Empty;
  5. if (HttpContext.Current.Request.HttpMethod.ToUpper() == "POST")
  6. {
  7. using (Stream stream = HttpContext.Current.Request.InputStream)
  8. {
  9. Byte[] postBytes = new Byte[stream.Length];
  10. stream.Read(postBytes, 0, (Int32)stream.Length);
  11. postString = Encoding.UTF8.GetString(postBytes);
  12. }
  13. if (!string.IsNullOrEmpty(postString))
  14. {
  15. ResponseXML(postString);//返回给微信用户信息
  16. }
  17. ///加载自定义菜单
  18. string postData = "{" + "\r\n";
  19. postData += "\"button\":[ " + "\r\n";
  20. postData += "{ " + "\r\n";
  21. postData += "\"name\":\"简单查\"," + "\r\n";
  22. postData += "\"sub_button\":[" + "\r\n";
  23. postData += "{ " + "\r\n";
  24. postData += " \"type\":\"click\"," + "\r\n";
  25. postData += " \"name\":\"我的薪资\", " + "\r\n";
  26. postData += " \"key\":\"mypay\"" + "\r\n";
  27. postData += "}," + "\r\n";
  28. postData += "{ " + "\r\n";
  29. postData += " \"type\":\"click\"," + "\r\n";
  30. postData += " \"name\":\"天气预报\", " + "\r\n";
  31. postData += " \"key\":\"tianqiyubao\"" + "\r\n";
  32. postData += "}," + "\r\n";
  33. postData += "{ " + "\r\n";
  34. postData += " \"type\":\"view\"," + "\r\n";
  35. postData += " \"name\":\"火车票查询\", " + "\r\n";
  36. postData += " \"url\":\"http://www.deqiaohr.com.cn/*******.aspx\"" + "\r\n";
  37. postData += "}," + "\r\n";
  38. postData += "{ " + "\r\n";
  39. postData += " \"type\":\"click\"," + "\r\n";
  40. postData += " \"name\":\"开心一刻\", " + "\r\n";
  41. postData += " \"key\":\"kaixinyixiao\"" + "\r\n";
  42. postData += " }]" + "\r\n";
  43. postData += "}," + "\r\n";
  44. postData += "{" + "\r\n";
  45. postData += "\"name\":\"会员管理\", " + "\r\n";
  46. postData += "\"sub_button\":[" + "\r\n";
  47. postData += "{ " + "\r\n";
  48. postData += " \"type\":\"view\"," + "\r\n";
  49. postData += " \"name\":\"会员注册\", " + "\r\n";
  50. postData += " \"url\":\"http://www.deqiaohr.com.cn/****.aspx\"" + "\r\n";
  51. postData += "}," + "\r\n";
  52. postData += "{ " + "\r\n";
  53. postData += " \"type\":\"view\"," + "\r\n";
  54. postData += " \"name\":\"重置密码\", " + "\r\n";
  55. postData += " \"url\":\"http://www.deqiaohr.com.cn/****.aspx\"" + "\r\n";
  56. postData += "}," + "\r\n";
  57. postData += "{ " + "\r\n";
  58. postData += " \"type\":\"click\"," + "\r\n";
  59. postData += " \"name\":\"修改资料\", " + "\r\n";
  60. postData += " \"key\":\"updateMessage\"" + "\r\n";
  61. postData += "}," + "\r\n";
  62. postData += "{ " + "\r\n";
  63. postData += " \"type\":\"click\"," + "\r\n";
  64. postData += " \"name\":\"我的提问\", " + "\r\n";
  65. postData += " \"key\":\"mywen\"" + "\r\n";
  66. postData += "}," + "\r\n";
  67. postData += "{ " + "\r\n";
  68. postData += " \"type\":\"click\"," + "\r\n";
  69. postData += " \"name\":\"联系客服\", " + "\r\n";
  70. postData += " \"key\":\"PhoneSerices\"" + "\r\n";
  71. postData += " }]" + "\r\n";
  72. postData += "}," + "\r\n";
  73. postData += "{" + "\r\n";
  74. postData += "\"name\":\"活动通知\"," + "\r\n";
  75. postData += "\"sub_button\":[" + "\r\n";
  76. postData += "{ " + "\r\n";
  77. postData += " \"type\":\"click\"," + "\r\n";
  78. postData += " \"name\":\"近期活动\", " + "\r\n";
  79. postData += " \"key\":\"yuangonghuodong\"" + "\r\n";
  80. postData += "}," + "\r\n";
  81. postData += "{ " + "\r\n";
  82. postData += " \"type\":\"click\"," + "\r\n";
  83. postData += " \"name\":\"近期通知\", " + "\r\n";
  84. postData += " \"key\":\"yuangongtongzhi\"" + "\r\n";
  85. postData += "}," + "\r\n";
  86. postData += "{ " + "\r\n";
  87. postData += " \"type\":\"click\"," + "\r\n";
  88. postData += " \"name\":\"有问必答\", " + "\r\n";
  89. postData += " \"key\":\"youwenbida\"" + "\r\n";
  90. postData += " }]" + "\r\n";
  91. postData += "}]" + "\r\n";
  92. postData += "}" + "\r\n";
  93. //自定义菜单token的获取 是用 下面的两个参数 获取的 不能直接用 公众平台的token
  94. string to = GetAccessToken();
  95. //本人不喜欢 后台 json的操作 直接截取就可以了 得到的就是 token 或者 自己 获取 json的token
  96. to = to.Substring(17, to.Length - 37);
  97. //加载菜单
  98. string i = GetPage("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + to, postData);
  99. }
  100. else
  101. {
  102. Auth(); //微信接入的测试
  103. }
  104. }
  105. /// <summary>
  106. /// 获取通行证
  107. /// </summary>
  108. /// <returns></returns>
  109. private string GetAccessToken()
  110. {
  111. string url_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=********&secret=*********";
  112. HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url_token);
  113. myRequest.Method = "GET";
  114. HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
  115. StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
  116. string content = reader.ReadToEnd();
  117. reader.Close();
  118. return content;
  119. }
  120. /// <summary>
  121. /// 加载菜单项
  122. /// </summary>
  123. /// <param name="p"></param>
  124. /// <param name="postData"></param>
  125. /// <returns></returns>
  126. private string GetPage(string p, string postData)
  127. {
  128. Stream outstream = null;
  129. Stream instream = null;
  130. StreamReader sr = null;
  131. HttpWebResponse response = null;
  132. HttpWebRequest request = null;
  133. Encoding encoding = Encoding.UTF8;
  134. byte[] data = encoding.GetBytes(postData);
  135. // 准备请求...
  136. try
  137. {
  138. // 设置参数
  139. request = WebRequest.Create(p) as HttpWebRequest;
  140. CookieContainer cookieContainer = new CookieContainer();
  141. request.CookieContainer = cookieContainer;
  142. request.AllowAutoRedirect = true;
  143. request.Method = "POST";
  144. request.ContentType = "application/x-www-form-urlencoded";
  145. request.ContentLength = data.Length;
  146. outstream = request.GetRequestStream();
  147. outstream.Write(data, 0, data.Length);
  148. outstream.Close();
  149. //发送请求并获取相应回应数据
  150. response = request.GetResponse() as HttpWebResponse;
  151. //直到request.GetResponse()程序才开始向目标网页发送Post请求
  152. instream = response.GetResponseStream();
  153. sr = new StreamReader(instream, encoding);
  154. //返回结果网页(html)代码
  155. string content = sr.ReadToEnd();
  156. string err = string.Empty;
  157. return content;
  158. }
  159. catch (Exception ex)
  160. {
  161. string err = ex.Message;
  162. return string.Empty;
  163. }
  164. }
  165. /// <summary>
  166. /// 获取参数进行认证
  167. /// </summary>
  168. private void Auth()
  169. {
  170. string token = "*****";//你申请的时候填写的Token
  171. string echoString = HttpContext.Current.Request.QueryString["echoStr"];
  172. string signature = HttpContext.Current.Request.QueryString["signature"];
  173. string timestamp = HttpContext.Current.Request.QueryString["timestamp"];
  174. string nonce = HttpContext.Current.Request.QueryString["nonce"];
  175. if (CheckSignature(token, signature, timestamp, nonce))
  176. {
  177. if (!string.IsNullOrEmpty(echoString))
  178. {
  179. HttpContext.Current.Response.Write(echoString);
  180. HttpContext.Current.Response.End();
  181. }
  182. }
  183. }
  184. /// <summary>
  185. /// 对微信传入参数进行封装到数组,拼接字符串,进行加密操作
  186. /// </summary>
  187. /// <param name="token"></param>
  188. /// <param name="signature"></param>
  189. /// <param name="timestamp"></param>
  190. /// <param name="nonce"></param>
  191. /// <returns></returns>
  192. private bool CheckSignature(string token, string signature, string timestamp, string nonce)
  193. {
  194. string[] ArrTmp = { token, timestamp, nonce };//将参数放进数组
  195. Array.Sort(ArrTmp);//对数组进行排序
  196. string tmpStr = string.Join("", ArrTmp);//将数组进行拼接
  197. ///对拼接后的字符串进行加密操作
  198. tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");
  199. //转换成小写形式
  200. tmpStr = tmpStr.ToLower();
  201. //比对成功返回
  202. if (tmpStr == signature)
  203. {
  204. return true;
  205. }
  206. else
  207. {
  208. return false;
  209. }
  210. }

精彩专题分享:ASP.NET微信开发教程汇总,欢迎大家学习。

以上就是关于asp.net微信开发的第一篇,针对开发者接入进行学习,之后会有更新更多关于asp.net微信开发的文章,希望大家持续关注。

人气教程排行