.NET 微信开放平台接口
文昌中立科技 | 2018-01-26 23:49:25 | 阅读:41345
<%@WebHandlerLanguage="C#"Class="WeixinInterface"%>usingSystem;usingSystem.Web;publicclassWeixinInterface: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;}returnEncryptToSHA1(combostr);}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