Html5移动开发论坛

注册

 

发新话题 回复该主题

.NET WCF 微信开放平台接口 [复制链接]

1#

参考:http://www.cnblogs.com/TivonStone/archive/2012/12/21/2827347.html

  1. /*cfxixi code 20130630 微信请求*/
  2.         public string GetWeixinInfo()
  3.         {
  4.             //WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*");

  5.             IncomingWebRequestContext request = WebOperationContext.Current.IncomingRequest;

  6.             var method = request.Method;

  7.             switch (method)
  8.             {
  9.                 case "POST":
  10.                     System.IO.Stream s = HttpContext.Current.Request.InputStream;
  11.                     byte[] b = new byte[s.Length];
  12.                     s.Read(b, 0, (int)s.Length);
  13.                     string postStr = System.Text.Encoding.UTF8.GetString(b);
  14.                     if (!string.IsNullOrEmpty(postStr))
  15.                     {
  16.                         return responseMsg(postStr);
  17.                     }
  18.                     return "POST...";
  19.                 case "DELETE":
  20.                     return "DELETE...";
  21.                 case "PUT":
  22.                     return "UPDATE...";
  23.                 default:
  24.                     WriteLog("before valid \n");
  25.                     return Valid(request);//用于验证
  26.             }
  27.         }

  28.         public string Valid(IncomingWebRequestContext request)
  29.         {
  30.             var echostr = request.UriTemplateMatch.QueryParameters["echoStr"];
  31.             if (checkSignature(request) && !string.IsNullOrEmpty(echostr))
  32.             {
  33.                 HttpContext.Current.Response.Write(echostr );
  34.                 HttpContext.Current.ApplicationInstance.CompleteRequest();//推送...不然微信平台无法验证token
  35.             }
  36.             return "error";
  37.         }

  38.         public bool checkSignature(IncomingWebRequestContext request)
  39.         {

  40.             //IncomingWebRequestContext request = WebOperationContext.Current.IncomingRequest;

  41.             var signature = request.UriTemplateMatch.QueryParameters["signature"];
  42.             var timestamp = request.UriTemplateMatch.QueryParameters["timestamp"];
  43.             var nonce = request.UriTemplateMatch.QueryParameters["nonce"];
  44.             var token = "fakeToken";
  45.             string[] ArrTmp = { token, timestamp, nonce };
  46.             Array.Sort(ArrTmp);     //字典排序
  47.             string tmpStr = string.Join("", ArrTmp);
  48.             tmpStr = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");
  49.             tmpStr = tmpStr.ToLower();
  50.             if (tmpStr == signature)
  51.             {
  52.                 return true;
  53.             }
  54.             else
  55.             {
  56.                 return false;
  57.             }
  58.         }

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

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

  78.         public string responseMsg(string postStr)
  79.         {
  80.             System.Xml.XmlDocument postObj = new System.Xml.XmlDocument();
  81.             postObj.LoadXml(postStr);
  82.             WriteLog("responseMsg:-------" + postStr);
  83.             var fromUserNameList = postObj.GetElementsByTagName("FromUserName");
  84.             string fromUserName = string.Empty;
  85.             for (int i = 0; i < fromUserNameList.Count; i++)
  86.             {
  87.                 if (fromUserNameList[i].ChildNodes[0].NodeType == System.Xml.XmlNodeType.CDATA)
  88.                 {
  89.                     fromUserName = fromUserNameList[i].ChildNodes[0].Value;
  90.                 }
  91.             }
  92.             var toUsernameList = postObj.GetElementsByTagName("ToUserName");
  93.             string toUserName = string.Empty;
  94.             for (int i = 0; i < toUsernameList.Count; i++)
  95.             {
  96.                 if (toUsernameList[i].ChildNodes[0].NodeType == System.Xml.XmlNodeType.CDATA)
  97.                 {
  98.                     toUserName = toUsernameList[i].ChildNodes[0].Value;
  99.                 }
  100.             }
  101.             var keywordList = postObj.GetElementsByTagName("Content");
  102.             string content = string.Empty;
  103.             for (int i = 0; i < keywordList.Count; i++)
  104.             {
  105.                 if (keywordList[i].ChildNodes[0].NodeType == System.Xml.XmlNodeType.CDATA)
  106.                 {
  107.                     content = keywordList[i].ChildNodes[0].Value;
  108.                 }
  109.             }
  110.             var time = DateTime.Now;
  111.             var textpl = "<xml><ToUserName><![CDATA[" + fromUserName + "]]></ToUserName>" +
  112.                 "<FromUserName><![CDATA[" + toUserName + "]]></FromUserName>" +
  113.                 "<CreateTime>" + ConvertDateTimeInt(DateTime.Now) + "</CreateTime><MsgType><![CDATA[text]]></MsgType>" +
  114.                 "<Content><![CDATA[欢迎来到微信世界---" + content + "]]></Content><FuncFlag>0</FuncFlag></xml> ";

  115.             HttpContext.Current.Response.Write(textpl );
  116.                 HttpContext.Current.ApplicationInstance.CompleteRequest();//推送...不然微信平台无法验证token
  117.         }

  118.         private int ConvertDateTimeInt(System.DateTime time)
  119.         {
  120.             System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
  121.             return (int)(time - startTime).TotalSeconds;
  122.         }

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

  150.         public bool IsReusable
  151.         {
  152.             get
  153.             {
  154.                 return false;
  155.             }
  156.         }

  157.         /*cfxixi code end*/
复制代码
分享 转发

Sencha Touch 交流 QQ 群 224711028 欢迎您的加入。
PhoneGap安卓 交流 QQ 群 250395324欢迎您的加入。
Html5 APP 交流 QQ 群 273843464欢迎您的加入。
TOP
2#

很好的帖子,赞一个!
TOP
3#

非常不错,学习了。
TOP
发新话题 回复该主题