版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、C#.net 微信公眾賬號接口開發(fā)_ 微信越來越火,微信公眾平臺成為開發(fā)成新寵,本文用C#.net開發(fā)微信公眾信號接口。 微信接口地址代碼: weixin _wx = new weixin(); string postStr = ; if (Request.HttpMethod.ToLower() = post) Stream s = System.Web.HttpContext.Current.Request.InputStream; byte b = new bytes.Length; s.Read(b, 0, (int)s.Length); postStr = Encoding.UTF8
2、.GetString(b); if (!string.IsNullOrEmpty(postStr) /懇求處理 _wx.Handle(postStr); else _wx.Auth(); 微信處理類: public class weixin private string Token = weixin_token; /換成自己的token public void Auth() string echoStr = System.Web.HttpContext.Current.Request.QueryStringechoStr; if (CheckSignature() if (!string.Is
3、NullOrEmpty(echoStr) System.Web.HttpContext.Current.Response.Write(echoStr); System.Web.HttpContext.Current.Response.End(); public void Handle(string postStr) /封裝懇求類 XmlDocument doc = new XmlDocument(); doc.LoadXml(postStr); XmlElement rootElement = doc.DocumentElement; XmlNode MsgType = rootElement
4、.SelectSingleNode(MsgType); RequestXML requestXML = new RequestXML(); requestXML.ToUserName = rootElement.SelectSingleNode(ToUserName).InnerText; requestXML.FromUserName = rootElement.SelectSingleNode(FromUserName).InnerText; requestXML.CreateTime = rootElement.SelectSingleNode(CreateTime).InnerText
5、; requestXML.MsgType = MsgType.InnerText; if (requestXML.MsgType = text) requestXML.Content = rootElement.SelectSingleNode(Content).InnerText; else if (requestXML.MsgType = location) requestXML.Location_X = rootElement.SelectSingleNode(Location_X).InnerText; requestXML.Location_Y = rootElement.Selec
6、tSingleNode(Location_Y).InnerText; requestXML.Scale = rootElement.SelectSingleNode(Scale).InnerText; requestXML.Label = rootElement.SelectSingleNode(Label).InnerText; else if (requestXML.MsgType = image) requestXML.PicUrl = rootElement.SelectSingleNode(PicUrl).InnerText; /回復消息 ResponseMsg(requestXML
7、); / summary / 驗證微信簽名 / /summary / * 將token、timestamp、nonce三個參數進行字典序排序 / * 將三個參數字符串拼接成一個字符串進行sha1加密 / * 開發(fā)者獲得加密后的字符串可與signature對比,標識該懇求來源于微信。 / returns/returns private bool CheckSignature() string signature = System.Web.HttpContext.Current.Request.QueryStringsignature; string timestamp = System.Web.
8、HttpContext.Current.Request.QueryStringtimestamp; string nonce = System.Web.HttpContext.Current.Request.QueryStringnonce; string ArrTmp = Token, timestamp, nonce ; Array.Sort(ArrTmp); /字典排序 string tmpStr = string.Join(, ArrTmp); tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr,
9、 SHA1); tmpStr = tmpStr.ToLower(); if (tmpStr = signature) return true; else return false; / summary / 回復消息(微信信息返回) / /summary / param name=weixinXML/param private void ResponseMsg(RequestXML requestXML) try string resxml = ; mijiya mi = new mijiya(requestXML.Content,requestXML.FromUserName); if (re
10、questXML.MsgType = text) /在這里執(zhí)行一系列操作,從而實現自動回復內容. string _reMsg = mi.GetReMsg(); if (mi.msgType = 1) resxml = xmlToUserName!CDATA + requestXML.FromUserName + /ToUserNameFromUserName!CDATA + requestXML.ToUserName + /FromUserNameCreateTime + ConvertDateTimeInt(DateTime.Now) + /CreateTimeMsgType!CDATAne
11、ws/MsgTypeContent!CDATA/ContentArticleCount2/ArticleCountArticles; resxml += mi.GetRePic(requestXML.FromUserName); resxml += /ArticlesFuncFlag1/FuncFlag/xml; else resxml = xmlToUserName!CDATA + requestXML.FromUserName + /ToUserNameFromUserName!CDATA + requestXML.ToUserName + /FromUserNameCreateTime
12、+ ConvertDateTimeInt(DateTime.Now) + /CreateTimeMsgType!CDATAtext/MsgTypeContent!CDATA + _reMsg + /ContentFuncFlag1/FuncFlag/xml; else if (requestXML.MsgType = location) string city = GetMapInfo(requestXML.Location_X, requestXML.Location_Y); if (city = 0) resxml = xmlToUserName!CDATA + requestXML.Fr
13、omUserName + /ToUserNameFromUserName!CDATA + requestXML.ToUserName + /FromUserNameCreateTime + ConvertDateTimeInt(DateTime.Now) + /CreateTimeMsgType!CDATAtext/MsgTypeContent!CDATA好啦,我們知道您的位置啦。您可以: + mi.GetDefault() + /ContentFuncFlag1/FuncFlag/xml; else resxml = xmlToUserName!CDATA + requestXML.From
14、UserName + /ToUserNameFromUserName!CDATA + requestXML.ToUserName + /FromUserNameCreateTime + ConvertDateTimeInt(DateTime.Now) + /CreateTimeMsgType!CDATAtext/MsgTypeContent!CDATA好啦,我們知道您的位置啦。您可以: + mi.GetDefault() + /ContentFuncFlag1/FuncFlag/xml; else if (requestXML.MsgType = image) resxml = xmlToUs
15、erName!CDATA + requestXML.FromUserName + /ToUserNameFromUserName!CDATA + requestXML.ToUserName + /FromUserNameCreateTime + ConvertDateTimeInt(DateTime.Now) + /CreateTimeMsgType!CDATAtext/MsgTypeContent!CDATA親,我沒有看懂你的意思。您可以: + mi.GetDefault() + /ContentFuncFlag1/FuncFlag/xml; /返回10以內條 /int size = 10;
16、 /resxml = xmlToUserName!CDATA + requestXML.FromUserName + /ToUserNameFromUserName!CDATA + requestXML.ToUserName + /FromUserNameCreateTime + ConvertDateTimeInt(DateTime.Now) + /CreateTimeMsgType!CDATAnews/MsgTypeContent!CDATA/ContentArticleCount + size + /ArticleCountArticles; /Liststring list = new
17、 Liststring(); /假如有20條查詢的返回結果 /for (int i = 0; i 20; i+) / / list.Add(1); / /string piclist = new string /Abstract_Pencil_Scribble_Background_Vector_main.jpg, /balloon_tree.jpg, /bloom.jpg, /colorful_flowers.jpg, /colorful_summer_flower.jpg, /fall.jpg, /fall_tree.jpg, /growing_flowers.jpg, /shoes_il
18、lustration.jpg, /splashed_tree.jpg ; /for (int i = 0; i size i list.Count; i+) / / resxml += itemTitle!CDATA沈陽-黑龍江/TitleDescription!CDATA元旦特價:¥300 市場價:¥400/DescriptionPicUrl!CDATA + + piclisti + /PicUrlUrl!CDATA/Url/item; / /resxml += /ArticlesFuncFlag1/FuncFlag/xml; else if (wx_tmsg.GetMsgCount(req
19、uestXML.FromUserName) = 0) resxml = xmlToUserName!CDATA + requestXML.FromUserName + /ToUserNameFromUserName!CDATA + requestXML.ToUserName + /FromUserNameCreateTime + ConvertDateTimeInt(DateTime.Now) + /CreateTimeMsgType!CDATAtext/MsgTypeContent!CDATA + mi.GetFirst() + /ContentFuncFlag1/FuncFlag/xml;
20、 else resxml = xmlToUserName!CDATA + requestXML.FromUserName + /ToUserNameFromUserName!CDATA + requestXML.ToUserName + /FromUserNameCreateTime + ConvertDateTimeInt(DateTime.Now) + /CreateTimeMsgType!CDATAtext/MsgTypeContent!CDATA親,我沒有看懂你的意思。您可以: + mi.GetDefault() + /ContentFuncFlag1/FuncFlag/xml; /W
21、riteTxt(resxml); System.Web.HttpContext.Current.Response.Write(resxml); WriteToDB(requestXML, resxml,mi.pid); catch (Exception ex) /WriteTxt(特別: + ex.Message + Struck: + ex.StackTrace.ToString(); wx_logs.MyInsert(特別: + ex.Message + Struck: + ex.StackTrace.ToString(); / summary / unix時間轉換為datetime /
22、/summary / param name=timeStamp/param / returns/returns private DateTime UnixTimeToTime(string timeStamp) DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1); long lTime = long.Parse(timeStamp + 0000000); TimeSpan toNow = new TimeSpan(lTime); return dtStart.Add(toNow); /
23、 summary / datetime轉換為unixtime / /summary / param name=time/param / returns/returns private int ConvertDateTimeInt(System.DateTime time) System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1); return (int)(time - startTime).TotalSeconds; / summary / 調用百度地圖,返
24、回坐標信息 / /summary / param name=y經度/param / param name=x緯度/param / returns/returns public string GetMapInfo(string x, string y) try string res = string.Empty; string parame = string.Empty; string url = parame = latlng= + x + , + y + language=zh-CNsensor=false;/此key為個人申請 res = webRequestPost(url, param
25、e); XmlDocument doc = new XmlDocument(); doc.LoadXml(res); XmlElement rootElement = doc.DocumentElement; string Status = rootElement.SelectSingleNode(status).InnerText; if (Status = OK) /僅獵取城市 XmlNodeList xmlResults = rootElement.SelectSingleNode(/GeocodeResponse).ChildNodes; for (int i = 0; i xmlRe
26、sults.Count; i+) XmlNode childNode = xmlResultsi; if (childNode.Name = status) continue; string city = 0; for (int w = 0; w childNode.ChildNodes.Count; w+) for (int q = 0; q childNode.ChildNodesw.ChildNodes.Count; q+) XmlNode childeTwo = childNode.ChildNodesw.ChildNodesq; if (childeTwo.Name = long_n
27、ame) city = childeTwo.InnerText; else if (childeTwo.InnerText = locality) return city; return city; catch (Exception ex) /WriteTxt(map特別: + ex.Message.ToString() + Struck: + ex.StackTrace.ToString(); return 0; return 0; / summary / Post 提交調用抓取 / /summary / param name=url提交地址/param / param name=param
28、參數/param / returnsstring/returns public string webRequestPost(string url, string param) byte bs = System.Text.Encoding.UTF8.GetBytes(param); HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url + ? + param); req.Method = Post; req.Timeout = 120 * 1000; req.ContentType = application/x-form-
29、urlencoded; req.ContentLength = bs.Length; using (Stream reqStream = req.GetRequestStream() reqStream.Write(bs, 0, bs.Length); reqStream.Flush(); using (WebResponse wr = req.GetResponse() /在這里對接收到的頁面內容進行處理 Stream strm = wr.GetResponseStream(); StreamReader sr = new StreamReader(strm, System.Text.Enc
30、oding.UTF8); string line; System.Text.StringBuilder sb = new System.Text.StringBuilder(); while (line = sr.ReadLine() != null) sb.Append(line + System.Environment.NewLine); sr.Close(); strm.Close(); return sb.ToString(); private void WriteToDB(RequestXML requestXML,string _xml,int _pid) wx_tmsg wx =
31、 new wx_tmsg(); wx.AddNew(); wx.FromUserName = requestXML.FromUserName; wx.ToUserName = requestXML.ToUserName; wx.MsgType = requestXML.MsgType; wx.Msg = requestXML.Content; wx.Creatime = requestXML.CreateTime; wx.Location_X = requestXML.Location_X; wx.Location_Y = requestXML.Location_Y; wx.Label = r
32、equestXML.Label; wx.Scale = requestXML.Scale; wx.PicUrl = requestXML.PicUrl; wx.reply = _xml; wx.pid = _pid; try wx.Update(); catch (Exception ex) wx_logs.MyInsert(ex.Message); /ex.message; /微信懇求類 public class RequestXML private string toUserName=; / summary / 消息接收方微信號,一般為公眾平臺賬號微信號 / /summary public string ToUserName get return toUserName; set toUserName = value; private string fromUserName = ; / summary / 消息發(fā)送方微信號 / /summary public string FromUserName get return fromUserName; set fromUserN
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024幼兒園門衛(wèi)消防應急處理聘用合同范本3篇
- 2024年軟裝飾品采購合作合同樣本版B版
- 2024年跨區(qū)域能源供應協(xié)議
- 2024年貨車司機就業(yè)協(xié)議
- 2024某網絡公司與某廣告公司之間關于網絡推廣服務的合同
- 2024年高端醫(yī)療設備采購與技術支持合同
- 2024年購房委托代理協(xié)議
- 2024電子產品模具加工與質量保證合同
- 2024年離異后財產處置協(xié)議:雙方協(xié)商達成
- 2025年度智能溫室大棚承包經營合同范本3篇
- 小學數學六年級解方程練習300題及答案
- 電抗器噪聲控制與減振技術
- 2024年醫(yī)療管理趨勢展望挑戰(zhàn)與機遇培訓課件
- 2024年江蘇揚州市高郵市國有企業(yè)招聘筆試參考題庫附帶答案詳解
- 內鏡下食管靜脈曲張?zhí)自g圍手術期護理課件
- 35江蘇省蘇州市2023-2024學年高一上學期期末學業(yè)質量陽光指標調研地理試卷
- 組態(tài)王與MySQL數據庫連接配置教程-20190807
- 運輸行業(yè)員工崗前安全培訓
- 《機械基礎(第七版)》期末考試復習題庫(含答案)
- 部編人教版語文九年級上冊文言文課下注釋
- 2023-2024學年滬科版九年級上學期物理期末模擬試卷(含答案)
評論
0/150
提交評論