c#.net微信公眾賬號接口開發(fā)_第1頁
c#.net微信公眾賬號接口開發(fā)_第2頁
c#.net微信公眾賬號接口開發(fā)_第3頁
c#.net微信公眾賬號接口開發(fā)_第4頁
c#.net微信公眾賬號接口開發(fā)_第5頁
已閱讀5頁,還剩8頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領

文檔簡介

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.g

2、etstring(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.isnul

3、lorempty(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.se

4、lectsinglenode(msgtype); requestxml requestxml = new requestxml(); requestxml.tousername = rootelement.selectsinglenode(tousername).innertext; requestxml.fromusername = rootelement.selectsinglenode(fromusername).innertext; requestxml.createtime = rootelement.selectsinglenode(createtime).innertext; r

5、equestxml.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.selectsi

6、nglenode(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、/ / 驗證微信簽名 / / * 將token、timestamp、nonce三個參數(shù)進行字典序排序 / * 將三個參數(shù)字符串拼接成一個字符串進行sha1加密 / * 開發(fā)者獲得加密后的字符串可與signature對比,標識該請求來源于微信。 / private bool checksignature() string signature = system.web.httpcontext.current.request.querystringsignature; string timestamp = system.web.httpcontext.current.request.querystr

8、ingtimestamp; 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, sha1); tmpstr = tmpstr.tolower(); i

9、f (tmpstr = signature) return true; else return false; / / 回復消息(微信信息返回) / / private void responsemsg(requestxml requestxml) try string resxml = ; mijiya mi = new mijiya(requestxml.content,requestxml.fromusername); if (requestxml.msgtype = text) /在這里執(zhí)行一系列操作,從而實現(xiàn)自動回復內(nèi)容. string _remsg = mi.getremsg();

10、if (mi.msgtype = 1) resxml = + convertdatetimeint(datetime.now) + 2; resxml += mi.getrepic(requestxml.fromusername); resxml += 1; else resxml = + convertdatetimeint(datetime.now) + 1; else if (requestxml.msgtype = location) string city = getmapinfo(requestxml.location_x, requestxml.location_y); if (

11、city = 0) resxml = + convertdatetimeint(datetime.now) + 1; else resxml = + convertdatetimeint(datetime.now) + 1; else if (requestxml.msgtype = image) resxml = + convertdatetimeint(datetime.now) + 1; /返回10以內(nèi)條 /int size = 10; /resxml = + convertdatetimeint(datetime.now) + + size + ; /list list = new l

12、ist(); /假如有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_illustrati

13、on.jpg, /splashed_tree.jpg ; /for (int i = 0; i size & i list.count; i+) / / resxml += ; / /resxml += 1; else if (wx_tmsg.getmsgcount(requestxml.fromusername) = 0) resxml = + convertdatetimeint(datetime.now) + 1; else resxml = + convertdatetimeint(datetime.now) + 1; /writetxt(resxml); system.web.htt

14、pcontext.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(); / / unix時間轉(zhuǎn)換為datetime / / / private datetime unixtimetotime(str

15、ing 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); / / datetime轉(zhuǎn)換為unixtime / / / private int convertdatetimeint(system.datetime time) system.da

16、tetime starttime = timezone.currenttimezone.tolocaltime(new system.datetime(1970, 1, 1); return (int)(time - starttime).totalseconds; / / 調(diào)用百度地圖,返回坐標信息 / / 經(jīng)度 / 緯度 / public string getmapinfo(string x, string y) try string res = string.empty; string parame = string.empty; string url = http:/maps.goog

17、/maps/api/geocode/xml; parame = latlng= + x + , + y + &language=zh-cn&sensor=false;/此key為個人申請 res = webrequestpost(url, parame); xmldocument doc = new xmldocument(); doc.loadxml(res); xmlelement rootelement = doc.documentelement; string status = rootelement.selectsinglenode(status).innerte

18、xt; if (status = ok) /僅獲取城市 xmlnodelist xmlresults = rootelement.selectsinglenode(/geocoderesponse).childnodes; for (int i = 0; i xmlresults.count; i+) xmlnode childnode = xmlresultsi; if (childnode.name = status) continue; string city = 0; for (int w = 0; w childnode.childnodes.count; w+) for (int

19、q = 0; q childnode.childnodesw.childnodes.count; q+) xmlnode childetwo = childnode.childnodesw.childnodesq; if (childetwo.name = long_name) city = childetwo.innertext; else if (childetwo.innertext = locality) return city; return city; catch (exception ex) /writetxt(map異常: + ex.message.tostring() + s

20、truck: + ex.stacktrace.tostring(); return 0; return 0; / / post 提交調(diào)用抓取 / / 提交地址 / 參數(shù) / string 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; re

21、q.timeout = 120 * 1000; req.contenttype = application/x-www-form-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)容進行處理 stream strm = wr.getresponses

22、tream(); streamreader sr = new streamreader(strm, system.text.encoding.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 w

23、ritetodb(requestxml requestxml,string _xml,int _pid) wx_tmsg wx = 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 = requestxm

24、l.location_x; wx.location_y = requestxml.location_y; wx.label = requestxml.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

25、 tousername=; / / 消息接收方微信號,一般為公眾平臺賬號微信號 / public string tousername get return tousername; set tousername = value; private string fromusername = ; / / 消息發(fā)送方微信號 / public string fromusername get return fromusername; set fromusername = value; private string createtime = ; / / 創(chuàng)建時間 / public string createtime get return createtime; set createtime = value; private string msgtype = ; / / 信息

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論