网站首页 网站建设 微信开发 网站推广 网站超市 客户案例 网站模板 关于我们
  首页 >> 微信开发 >> 开发技术资讯
.NET 微信开放平台接口
  1. <%@ WebHandler Language="C#" Class="WeixinInterface" %>

  2. using System;
  3. using System.Web;

  4. public class WeixinInterface : IHttpHandler
  5. {
  6. HttpContext context = null;
  7. string postStr = "";
  8. public void ProcessRequest(HttpContext param_context)
  9. {
  10. context = param_context;

  11. //以写日志为荣,以打断点为耻.
  12. //WriteLog("before valid n");
  13. //valid();//用于验证
  14. //WriteLog("after valid, before post n");
  15. if (context.Request.HttpMethod.ToLower() == "post")
  16. {
  17. System.IO.Stream s = context.Request.InputStream;
  18. byte[] b = new byte[s.Length];
  19. s.Read(b, 0, (int)s.Length);
  20. postStr = System.Text.Encoding.UTF8.GetString(b);
  21. if (!string.IsNullOrEmpty(postStr))
  22. {
  23. responseMsg(postStr);
  24. }
  25. //WriteLog("-------AfterResponseMsg:-------n" + postStr);
  26. }
  27. }

  28. public void valid()
  29. {
  30. var echostr = context.Request["echoStr"].ToString();
  31. if (checkSignature() && !string.IsNullOrEmpty(echostr))
  32. {
  33. context.Response.Write(echostr);
  34. context.Response.End();//推送...不然微信平台无法验证token
  35. }
  36. }

  37. public bool checkSignature()
  38. {
  39. var signature = context.Request["signature"].ToString();
  40. var timestamp = context.Request["timestamp"].ToString();
  41. var nonce = context.Request["nonce"].ToString();
  42. var token = "faketoken";
  43. string[] ArrTmp = { token, timestamp, nonce };
  44. Array.Sort(ArrTmp); //字典排序
  45. string tmpStr = string.Join("", ArrTmp);
  46. tmpStr = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");
  47. tmpStr = tmpStr.ToLower();
  48. if (tmpStr == signature)
  49. {
  50. return true;
  51. }
  52. else
  53. {
  54. return false;
  55. }
  56. }

  57. public string GetSha1(System.Collections.Generic.List codelist)
  58. {
  59. codelist.Sort();
  60. var combostr = string.Empty;
  61. for (int i = 0; i < codelist.Count; i++)
  62. {
  63. combostr += codelist;
  64. }
  65. return EncryptToSHA1(combostr);
  66. }

  67. public string EncryptToSHA1(string str)
  68. {
  69. System.Security.Cryptography.SHA1CryptoServiceProvider sha1 = new System.Security.Cryptography.SHA1CryptoServiceProvider();
  70. byte[] str1 = System.Text.Encoding.UTF8.GetBytes(str);
  71. byte[] str2 = sha1.ComputeHash(str1);
  72. sha1.Clear();
  73. (sha1 as IDisposable).Dispose();
  74. return Convert.ToBase64String(str2);
  75. }

  76. public void responseMsg(string postStr)
  77. {
  78. System.Xml.XmlDocument postObj = new System.Xml.XmlDocument();
  79. postObj.LoadXml(postStr);
  80. WriteLog("responseMsg:-------" + postStr);
  81. var FromUserNameList = postObj.GetElementsByTagName("FromUserName");
  82. string FromUserName = string.Empty;
  83. for (int i = 0; i < FromUserNameList.Count; i++)
  84. {
  85. if (FromUserNameList.ChildNodes[0].NodeType == System.Xml.XmlNodeType.CDATA)
  86. {
  87. FromUserName = FromUserNameList.ChildNodes[0].Value;
  88. }
  89. }
  90. var toUsernameList = postObj.GetElementsByTagName("ToUserName");
  91. string ToUserName = string.Empty;
  92. for (int i = 0; i < toUsernameList.Count; i++)
  93. {
  94. if (toUsernameList.ChildNodes[0].NodeType == System.Xml.XmlNodeType.CDATA)
  95. {
  96. ToUserName = toUsernameList.ChildNodes[0].Value;
  97. }
  98. }
  99. var keywordList = postObj.GetElementsByTagName("Content");
  100. string Content = string.Empty;
  101. for (int i = 0; i < keywordList.Count; i++)
  102. {
  103. if (keywordList.ChildNodes[0].NodeType == System.Xml.XmlNodeType.CDATA)
  104. {
  105. Content = keywordList.ChildNodes[0].Value;
  106. }
  107. }
  108. var time = DateTime.Now;
  109. var textpl = "" +
  110. "" +
  111. "" + ConvertDateTimeInt(DateTime.Now) + "" +
  112. "0 ";
  113. context.Response.Write(textpl);
  114. context.Response.End();
  115. }

  116. private DateTime UnixTimeToTime(string timeStamp)
  117. {
  118. DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
  119. long lTime = long.Parse(timeStamp + "0000000");
  120. TimeSpan toNow = new TimeSpan(lTime);
  121. return dtStart.Add(toNow);
  122. }

  123. private int ConvertDateTimeInt(System.DateTime time)
  124. {
  125. System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
  126. return (int)(time - startTime).TotalSeconds;
  127. }

  128. private void WriteLog(string strMemo)
  129. {
  130. string filename = "D:/WEBHOME/logs/log.txt";
  131. if (!System.IO.Directory.Exists("D:/WEBHOME/logs/"))
  132. System.IO.Directory.CreateDirectory("D:/WEBHOME/logs/");
  133. System.IO.StreamWriter sr = null;
  134. try
  135. {
  136. if (!System.IO.File.Exists(filename))
  137. {
  138. sr = System.IO.File.CreateText(filename);
  139. }
  140. else
  141. {
  142. sr = System.IO.File.AppendText(filename);
  143. }
  144. sr.WriteLine(strMemo);
  145. }
  146. catch
  147. {
  148. }
  149. finally
  150. {
  151. if (sr != null)
  152. sr.Close();
  153. }
  154. }

  155. public bool IsReusable
  156. {
  157. get
  158. {
  159. return false;
  160. }
  161. }
  162. }
复制代码
1.如果是为了验证微信接口的token是否通过, 将valid注释去掉

2.如果要返回给用户值, 注释掉valid
微信系统开发 微信开发功能 公众号基础教程 开发技术资讯 公众号推广营销 客户案例
地址:海南省海口市南宝路明都大厦1007#  
版权所有:海口世纪华联科技有限公司  设计制作:中立科技
菜单案例电话短信联系