版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、C#.net 微信公眾賬號(hào)接口開發(fā)_ 微信越來(lái)越火,微信公眾平臺(tái)成為開發(fā)成新寵,本文用C#.net開發(fā)微信公眾信號(hào)接口。 微信接口地址代碼: 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; /回復(fù)消息 ResponseMsg(requestXML
7、); / summary / 驗(yàn)證微信簽名 / /summary / * 將token、timestamp、nonce三個(gè)參數(shù)進(jìn)行字典序排序 / * 將三個(gè)參數(shù)字符串拼接成一個(gè)字符串進(jìn)行sha1加密 / * 開發(fā)者獲得加密后的字符串可與signature對(duì)比,標(biāo)識(shí)該懇求來(lái)源于微信。 / 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 / 回復(fù)消息(微信信息返回) / /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í)行一系列操作,從而實(shí)現(xiàn)自動(dòng)回復(fù)內(nèi)容. 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親,我沒(méi)有看懂你的意思。您可以: + mi.GetDefault() + /ContentFuncFlag1/FuncFlag/xml; /返回10以內(nèi)條 /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條查詢的返回結(jié)果 /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沈陽(yáng)-黑龍江/TitleDescription!CDATA元旦特價(jià):¥300 市場(chǎng)價(jià):¥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親,我沒(méi)有看懂你的意思。您可以: + 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時(shí)間轉(zhuǎn)換為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轉(zhuǎn)換為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 / 調(diào)用百度地圖,返
24、回坐標(biāo)信息 / /summary / param name=y經(jīng)度/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為個(gè)人申請(qǐng) 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 提交調(diào)用抓取 / /summary / param name=url提交地址/param / param name=param
28、參數(shù)/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() /在這里對(duì)接收到的頁(yè)面內(nèi)容進(jìn)行處理 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 / 消息接收方微信號(hào),一般為公眾平臺(tái)賬號(hào)微信號(hào) / /summary public string ToUserName get return toUserName; set toUserName = value; private string fromUserName = ; / summary / 消息發(fā)送方微信號(hào) / /summary public string FromUserName get return fromUserName; set fromUserN
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 地下室除患攻堅(jiān)大整治行動(dòng)工作總結(jié)
- 混凝土泵車事故調(diào)查報(bào)告
- 基于PLC的供水管網(wǎng)漏損檢測(cè)系統(tǒng)研究
- 人生的演講稿
- 周年慶典活動(dòng)方案
- 一院院本級(jí)離退休人員健康體檢項(xiàng)目
- 膜結(jié)構(gòu)車棚施工方案
- 斜坡道施工質(zhì)量檢測(cè)方案
- 中學(xué)科技創(chuàng)新周活動(dòng)方案
- 懷化學(xué)院《體育測(cè)量與評(píng)價(jià)》2021-2022學(xué)年第一學(xué)期期末試卷
- 污水處理站施工組織設(shè)計(jì)-完整版
- 經(jīng)濟(jì)日用文書-條據(jù)告啟
- 鏟車考試題庫(kù)
- 2022年公務(wù)員聯(lián)考公安專業(yè)科目真題與答案
- 《物聯(lián)網(wǎng)應(yīng)用系統(tǒng)開發(fā)》課程標(biāo)準(zhǔn)
- 防靜電標(biāo)準(zhǔn)規(guī)范
- 醫(yī)護(hù)人員個(gè)人防護(hù)和手衛(wèi)生的重要性
- 農(nóng)業(yè)昆蟲分類-螨類
- 2023年副主任醫(yī)師(副高)-中醫(yī)外科學(xué)(副高)考試歷年高頻考點(diǎn)真題附帶含答案
- 《旅游概論》課程標(biāo)準(zhǔn)
- 軍事理論知到章節(jié)答案智慧樹2023年哈爾濱工程大學(xué)
評(píng)論
0/150
提交評(píng)論