版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
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; /回復(fù)消息 ResponseMsg(requestXML
7、); / summary / 驗證微信簽名 / /summary / * 將token、timestamp、nonce三個參數(shù)進(jìn)行字典序排序 / * 將三個參數(shù)字符串拼接成一個字符串進(jìn)行sha1加密 / * 開發(fā)者獲得加密后的字符串可與signature對比,標(biāo)識該懇求來源于微信。 / 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í)行一系列操作,從而實現(xiàn)自動回復(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親,我沒有看懂你的意思。您可以: + 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沈陽-黑龍江/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時間轉(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為個人申請 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() /在這里對接收到的頁面內(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 / 消息接收方微信號,一般為公眾平臺賬號微信號 / /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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年跨境電商平臺入駐及貨款墊付合作協(xié)議3篇
- 2025版科技創(chuàng)新反擔(dān)保合同與研發(fā)設(shè)備抵押協(xié)議3篇
- 醫(yī)院與保險公司合同管理
- 畜牧業(yè)發(fā)展承諾書網(wǎng)上填報
- 廢舊輪胎處理合同
- 藝術(shù)空間租賃協(xié)議
- 消防安全評估防水施工合同
- 古玩市場物業(yè)員工招聘合同
- 個人工作室客戶意見箱管理方案
- 森林防火維護(hù)爆炸品庫房管理方案
- 2024版國開電大本科《行政領(lǐng)導(dǎo)學(xué)》在線形考(形考任務(wù)一至四)試題及答案
- 黑龍江省雞西市2023-2024學(xué)年上學(xué)期第二次質(zhì)量監(jiān)測九年級(五四年制)英語試卷
- 風(fēng)電教育培訓(xùn)體系建設(shè)
- 《機械基礎(chǔ)(第七版)》期末考試復(fù)習(xí)題庫(含答案)
- 鄉(xiāng)村振興與創(chuàng)新創(chuàng)業(yè):激發(fā)農(nóng)村創(chuàng)新創(chuàng)業(yè)活力
- 校園修繕施工方案
- 2018年全國統(tǒng)一施工機械臺班費用定額
- 2023年中考語文一輪復(fù)習(xí):童話示例與訓(xùn)練
- 軟裝設(shè)計合同范本
- 幼兒園人事工作計劃
- 廣東省深圳市福田區(qū)福田八校2023-2024學(xué)年九年級上學(xué)期開學(xué)道德與法治試題
評論
0/150
提交評論