真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

.net如何實(shí)現(xiàn)微信公眾賬號(hào)接口開發(fā)-創(chuàng)新互聯(lián)

這篇文章將為大家詳細(xì)講解有關(guān).net如何實(shí)現(xiàn)微信公眾賬號(hào)接口開發(fā),小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

網(wǎng)站建設(shè)、基于HTML5建站技術(shù)的Web開發(fā)、手機(jī)站開發(fā)、微信開發(fā)等互聯(lián)網(wǎng)應(yīng)用服務(wù)。成都創(chuàng)新互聯(lián)公司始終關(guān)注著互聯(lián)網(wǎng)行業(yè)的前沿動(dòng)態(tài),創(chuàng)新互聯(lián)堅(jiān)信:真誠的態(tài)度,勤奮的工作是我們贏得客戶信賴的基礎(chǔ);而不斷創(chuàng)新、力求完美,才是創(chuàng)新互聯(lián)共同邁向美好未來的保證。

說起微信公眾帳號(hào),大家都不會(huì)陌生,使用這個(gè)平臺(tái)能給網(wǎng)站或系統(tǒng)增加一個(gè)新亮點(diǎn),直接進(jìn)入正題吧,在使用之前一定要仔細(xì)閱讀官方API文檔。
API文檔地址:http://mp.weixin.qq.com/wiki/index.php

使用.net實(shí)現(xiàn)的方法:
//微信接口地址 頁面代碼:

代碼如下:



weixin _wx = new weixin(); 
string postStr = ""; 
if (Request.HttpMethod.ToLower() == "post") 

Stream s = System.Web.HttpContext.Current.Request.InputStream; 
byte[] b = new byte[s.Length]; 
s.Read(b, 0, (int)s.Length); 
postStr = Encoding.UTF8.GetString(b); 
if (!string.IsNullOrEmpty(postStr)) //請(qǐng)求處理 

_wx.Handle(postStr);  


else

_wx.Auth(); 
}


具體處理類

代碼如下:



///

 
/// 微信公眾平臺(tái)操作類 
///
 
public class weixin 

private string Token = "my_weixin_token"; //換成自己的token 
public void Auth() 

string echoStr = System.Web.HttpContext.Current.Request.QueryString["echoStr"]; 
if (CheckSignature()) //校驗(yàn)簽名是否正確 

if (!string.IsNullOrEmpty(echoStr)) 

System.Web.HttpContext.Current.Response.Write(echoStr); //返回原值表示校驗(yàn)成功 
System.Web.HttpContext.Current.Response.End(); 




 
public void Handle(string postStr) 

//封裝請(qǐng)求類 
XmlDocument doc = new XmlDocument(); 
doc.LoadXml(postStr); 
XmlElement rootElement = doc.DocumentElement; 
//MsgType 
XmlNode MsgType = rootElement.SelectSingleNode("MsgType"); 
//接收的值--->接收消息類(也稱為消息推送) 
RequestXML requestXML = new RequestXML(); 
requestXML.ToUserName = rootElement.SelectSingleNode("ToUserName").InnerText; 
requestXML.FromUserName = rootElement.SelectSingleNode("FromUserName").InnerText; 
requestXML.CreateTime = rootElement.SelectSingleNode("CreateTime").InnerText; 
requestXML.MsgType = MsgType.InnerText; 

//根據(jù)不同的類型進(jìn)行不同的處理 
switch (requestXML.MsgType) 

case "text": //文本消息 
requestXML.Content = rootElement.SelectSingleNode("Content").InnerText; 
break; 
case "image": //圖片 
requestXML.PicUrl = rootElement.SelectSingleNode("PicUrl").InnerText; 
break; 
case "location": //位置 
requestXML.Location_X = rootElement.SelectSingleNode("Location_X").InnerText; 
requestXML.Location_Y = rootElement.SelectSingleNode("Location_Y").InnerText; 
requestXML.Scale = rootElement.SelectSingleNode("Scale").InnerText; 
requestXML.Label = rootElement.SelectSingleNode("Label").InnerText; 
break; 
case "link": //鏈接 
break; 
case "event": //事件推送 支持V4.5+ 
break; 


//消息回復(fù) 
ResponseMsg(requestXML); 


 
///  
/// 驗(yàn)證微信簽名 
/// * 將token、timestamp、nonce三個(gè)參數(shù)進(jìn)行字典序排序 
/// * 將三個(gè)參數(shù)字符串拼接成一個(gè)字符串進(jìn)行sha1加密 
/// * 開發(fā)者獲得加密后的字符串可與signature對(duì)比,標(biāo)識(shí)該請(qǐng)求來源于微信。 
///
 
///  
private bool CheckSignature() 

string signature = System.Web.HttpContext.Current.Request.QueryString["signature"]; 
string timestamp = System.Web.HttpContext.Current.Request.QueryString["timestamp"]; 
string nonce = System.Web.HttpContext.Current.Request.QueryString["nonce"]; 
//加密/校驗(yàn)流程: 
//1. 將token、timestamp、nonce三個(gè)參數(shù)進(jìn)行字典序排序 
string[] ArrTmp = { Token, timestamp, nonce };  
Array.Sort(ArrTmp);//字典排序 
//2.將三個(gè)參數(shù)字符串拼接成一個(gè)字符串進(jìn)行sha1加密 
string tmpStr = string.Join("", ArrTmp); 
tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1"); 
tmpStr = tmpStr.ToLower(); 
//3.開發(fā)者獲得加密后的字符串可與signature對(duì)比,標(biāo)識(shí)該請(qǐng)求來源于微信。 
if (tmpStr == signature) 

return true; 

else

return false; 



///  
/// 消息回復(fù)(微信信息返回) 
///
 
/// The request XML. 
private void ResponseMsg(RequestXML requestXML) 

try

string resxml = ""; 
//主要是調(diào)用數(shù)據(jù)庫進(jìn)行關(guān)鍵詞匹配自動(dòng)回復(fù)內(nèi)容,可以根據(jù)自己的業(yè)務(wù)情況編寫。 
//1.通常有,沒有匹配任何指令時(shí),返回幫助信息 
AutoResponse mi = new AutoResponse(requestXML.Content, requestXML.FromUserName); 

switch (requestXML.MsgType) 

case "text": 
//在這里執(zhí)行一系列操作,從而實(shí)現(xiàn)自動(dòng)回復(fù)內(nèi)容.  
string _reMsg = mi.GetReMsg(); 
if (mi.msgType == 1) 

resxml = "" + ConvertDateTimeInt(DateTime.Now) + "2"; 
resxml += mi.GetRePic(requestXML.FromUserName); 
resxml += "
1
"; 

else

resxml = "" + ConvertDateTimeInt(DateTime.Now) + "1"; 

break; 
case "location": 
string city = GetMapInfo(requestXML.Location_X, requestXML.Location_Y); 
if (city == "0") 

resxml = "" + ConvertDateTimeInt(DateTime.Now) + "1"; 

else

resxml = "" + ConvertDateTimeInt(DateTime.Now) + "1"; 

break; 
case "image": 
//圖文混合的消息 具體格式請(qǐng)見官方API“回復(fù)圖文消息”  
break; 


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()); 



 
///  
/// unix時(shí)間轉(zhuǎn)換為datetime 
///
 
///  
///  
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); 


 
///  
/// datetime轉(zhuǎn)換為unixtime 
///
 
///  
///  
private int ConvertDateTimeInt(System.DateTime time) 

System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); 
return (int)(time - startTime).TotalSeconds; 


 
///  
/// 調(diào)用百度地圖,返回坐標(biāo)信息 
///
 
/// 經(jīng)度 
/// 緯度 
///  
public string GetMapInfo(string x, string y) 

try

string res = string.Empty; 
string parame = string.Empty; 
string url = "http://maps.googleapis.com/maps/api/geocode/xml"; 

parame = "latlng=" + x + "," + y + "&language=zh-CN&sensor=false";//此key為個(gè)人申請(qǐng) 
res = webRequestPost(url, parame); 

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 < xmlResults.Count; i++) 

XmlNode childNode = xmlResults[i]; 
if (childNode.Name == "status") { 
continue; 

string city = "0"; 
for (int w = 0; w < childNode.ChildNodes.Count; w++) 

for (int q = 0; q < childNode.ChildNodes[w].ChildNodes.Count; q++) 

XmlNode childeTwo = childNode.ChildNodes[w].ChildNodes[q]; 
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() + "Struck:" + 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"; 
req.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()) 

//在這里對(duì)接收到的頁面內(nèi)容進(jìn)行處理 
Stream strm = wr.GetResponseStream(); 
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(); 



///  
/// 將本次交互信息保存至數(shù)據(jù)庫中 
///
 
///  
///  
///  
private void WriteToDB(RequestXML requestXML, string _xml, int _pid) 

WeiXinMsg wx = new WeiXinMsg(); 
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 = requestXML.Label; 
wx.Scale = requestXML.Scale; 
wx.PicUrl = requestXML.PicUrl; 
wx.reply = _xml; 
wx.pid = _pid; 
try

wx.Add(); 

catch (Exception ex) 

//wx_logs.MyInsert(ex.Message); 
//ex.message; 


}


響應(yīng)類MODEL

復(fù)制代碼 代碼如下:


#region 微信請(qǐng)求類 RequestXML 
///

 
/// 微信請(qǐng)求類 
///
 
public class RequestXML 

private string toUserName = ""; 
///  
/// 消息接收方微信號(hào),一般為公眾平臺(tái)賬號(hào)微信號(hào) 
///
 
public string ToUserName 

get { return toUserName; } 
set { toUserName = value; } 


private string fromUserName = ""; 
///  
/// 消息發(fā)送方微信號(hào) 
///
 
public string FromUserName 

get { return fromUserName; } 
set { fromUserName = value; } 


private string createTime = ""; 
///  
/// 創(chuàng)建時(shí)間 
///
 
public string CreateTime 

get { return createTime; } 
set { createTime = value; } 


private string msgType = ""; 
///  
/// 信息類型 地理位置:location,文本消息:text,消息類型:image 
///
 
public string MsgType 

get { return msgType; } 
set { msgType = value; } 


private string content = ""; 
///  
/// 信息內(nèi)容 
///
 
public string Content 

get { return content; } 
set { content = value; } 


private string location_X = ""; 
///  
/// 地理位置緯度 
///
 
public string Location_X 

get { return location_X; } 
set { location_X = value; } 


private string location_Y = ""; 
///  
/// 地理位置經(jīng)度 
///
 
public string Location_Y 

get { return location_Y; } 
set { location_Y = value; } 


private string scale = ""; 
///  
/// 地圖縮放大小 
///
 
public string Scale 

get { return scale; } 
set { scale = value; } 


private string label = ""; 
///  
/// 地理位置信息 
///
 
public string Label 

get { return label; } 
set { label = value; } 


private string picUrl = ""; 
///  
/// 圖片鏈接,開發(fā)者可以用HTTP GET獲取 
///
 
public string PicUrl 

get { return picUrl; } 
set { picUrl = value; } 


#endregion


關(guān)于“.net如何實(shí)現(xiàn)微信公眾賬號(hào)接口開發(fā)”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。


網(wǎng)頁標(biāo)題:.net如何實(shí)現(xiàn)微信公眾賬號(hào)接口開發(fā)-創(chuàng)新互聯(lián)
網(wǎng)頁URL:http://weahome.cn/article/dgpgdi.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部