.NET 手机微信对外开放服务平台插口

 您如今的部位:  >> 手机微信开发设计 >> 开发设计技术性新闻资讯 开发设计技术性新闻资讯
.NET 手机微信对外开放服务平台插口 海口市保持中立高新科技 | :25 | 阅读文章:7388
%@WebHandlerLanguage="C#" % usingSystem;usingSystem.lassWeixinInterface:IHttpHandler{HttpContextcontext=null;stringpostStr="";publicvoidProcessRequest(HttpContextparam_context){context=param_context;//以写系统日志为荣,以切断点为耻.//WriteLog("beforevalidn");//valid();//用以认证//WriteLog("aftervalid,beforepostn");if(context.Request.HttpMethod.ToLower()=="post"){System.IO.Streams=context.Request.InputStream;byte[]b=newbyte[s.Length];s.Read(b,0,(int)s.Length);postStr=System.Text.Encoding.UTF8.GetString(b);if(!string.IsNullOrEmpty(postStr)){responseMsg(postStr);}//WriteLog("-------AfterResponseMsg:-------n"+postStr);}}publicvoidvalid(){varechostr=context.Request["echoStr"].ToString();if(checkSignature() !string.IsNullOrEmpty(echostr)){context.Response.Write(echostr);context.Response.End();//消息推送...要不然手机微信服务平台没法认证token}}publicboolcheckSignature(){varsignature=context.Request["signature"].ToString();vartimestamp=context.Request["timestamp"].ToString();varnonce=context.Request["nonce"].ToString();vartoken="faketoken";string[]ArrTmp={token,timestamp,nonce};Array.Sort(ArrTmp);//字典排列stringtmpStr=string.Join("",ArrTmp);tmpStr=System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr,"SHA1");tmpStr=tmpStr.ToLower();if(tmpStr==signature){returntrue;}else{returnfalse;}}publicstringGetSha1(System.Collections.Generic.Listcodelist){codelist.Sort();varcombostr=string.Empty;for(inti=0;i codelist.Count;i++){combostr+=codelist;}bostr);}publicstringEncryptToSHA1(stringstr){System.Security.Cryptography.SHA1CryptoServiceProvidersha1=newSystem.Security.Cryptography.SHA1CryptoServiceProvider();byte[]str1=System.Text.Encoding.UTF8.GetBytes(str);byte[]str2=sha1.ComputeHash(str1);sha1.Clear();(sha1asIDisposable).Dispose();returnConvert.ToBase64String(str2);}publicvoidresponseMsg(stringpostStr){System.Xml.XmlDocumentpostObj=newSystem.Xml.XmlDocument();postObj.LoadXml(postStr);WriteLog("responseMsg:-------"+postStr);varFromUserNameList=postObj.GetElementsByTagName("FromUserName");stringFromUserName=string.Empty;for(inti=0;i FromUserNameList.Count;i++){if(FromUserNameList.ChildNodes[0].NodeType==System.Xml.XmlNodeType.CDATA){FromUserName=FromUserNameList.ChildNodes[0].Value;}}vartoUsernameList=postObj.GetElementsByTagName("ToUserName");stringToUserName=string.Empty;for(inti=0;i toUsernameList.Count;i++){if(toUsernameList.ChildNodes[0].NodeType==System.Xml.XmlNodeType.CDATA){ToUserName=toUsernameList.ChildNodes[0].Value;}}varkeywordList=postObj.GetElementsByTagName("Content");stringContent=string.Empty;for(inti=0;i keywordList.Count;i++){if(keywordList.ChildNodes[0].NodeType==System.Xml.XmlNodeType.CDATA){Content=keywordList.ChildNodes[0].Value;}}vartime=DateTime.Now;vartextpl=""+""+""+ConvertDateTimeInt(DateTime.Now)+""+"0";context.Response.Write(textpl);context.Response.End();}privateDateTimeUnixTimeToTime(stringtimeStamp){DateTimedtStart=TimeZone.CurrentTimeZone.ToLocalTime(newDateTime(1970,1,1));longlTime=long.Parse(timeStamp+"0000000");TimeSpantoNow=newTimeSpan(lTime);returndtStart.Add(toNow);}privateintConvertDateTimeInt(System.DateTimetime){System.DateTimestartTime=TimeZone.CurrentTimeZone.ToLocalTime(newSystem.DateTime(1970,1,1));return(int)(time-startTime).TotalSeconds;}privatevoidWriteLog(stringstrMemo){stringfilename="D:/WEBHOME/logs/log.txt";if(!System.IO.Directory.Exists("D:/WEBHOME/logs/"))System.IO.Directory.CreateDirectory("D:/WEBHOME/logs/");System.IO.StreamWritersr=null;try{if(!System.IO.File.Exists(filename)){sr=System.IO.File.CreateText(filename);}else{sr=System.IO.File.AppendText(filename);}sr.WriteLine(strMemo);}catch{}finally{if(sr!=null)sr.Close();}}publicboolIsReusable{get{returnfalse;}}}拷贝编码1.假如是以便认证手机微信插口的token是不是根据,将valid注解除掉2.假如要回到给客户值,注解掉valid