版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
【移動(dòng)應(yīng)用開發(fā)技術(shù)】C#中.net微信開發(fā)的示例分析
string[]
ArrTmp
=
{
"token",
Request["timestamp"],
Request["nonce"]
};
Array.Sort(ArrTmp);
//字典排序string
tmpStr
=
string.Join("",
ArrTmp);
tmpStr
=
FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr,
"SHA1");
tmpStr
=
tmpStr.ToLower();if
(tmpStr
!=
Request["signature"].ToLower())
{
Response.Write(Request["echostr"]);
}/upload/information/20201208/260/14946.jpg/upload/information/20201208/260/14947.jpg/upload/information/20201208/260/14948.jpg/upload/information/20201208/260/14949.jpgnamespace
ElegantWM.WebUI.Areas.WeiXin.Controllers
{
///
<summary>
///
微信公共服務(wù)類
///
</summary>
public
class
RobotController
:
BaseController
{
///
<summary>
///
接收微信請(qǐng)求接口,認(rèn)證的接口
///
</summary>
///
<returns></returns>
public
ContentResult
Index()
{
string
result
=
AnalyzeXmlFromWeiXin();
return
Content(result);
}
///
<summary>
///
分析微信POST到本服務(wù)器的XML數(shù)據(jù)
///
</summary>
///
<param
name="xmlStream"></param>
///
<returns></returns>
private
string
AnalyzeXmlFromWeiXin()
{
StreamReader
reader
=
new
StreamReader(Request.InputStream);
string
xml
=
reader.ReadToEnd();
//獲取MsgType
log.Info(xml);
string
msgType
=
XmlHelper.ReadXmlStr(xml,
"/xml/MsgType",
"");
switch
(msgType)
{
case
"event"://如果是事件
return
OnAttention(xml);
case
"text"://如果是文本消息
return
OnReceiveTextMsg(xml);
default:
return
"不被支持的關(guān)鍵字!";
}
}
///
<summary>
///
響應(yīng)文本消息
///
</summary>
///
<param
name="xml"></param>
///
<returns></returns>
private
string
OnReceiveTextMsg(string
xml)
{
WxTextMsg
msg
=
XmlEntityExchange<WxTextMsg>.ConvertXml2Entity(xml);
//返回消息,互換收發(fā)用戶
string
toUser
=
msg.FromUserName;
msg.FromUserName
=
msg.ToUserName;
msg.ToUserName
=
toUser;
switch
(msg.Content.Trim())
{
case
"?":
msg.Content
=
HelpDocument();
break;
case
"?":
msg.Content
=
HelpDocument();
break;
case
"1":
msg.Content
=
DateTime.Now.ToString("yyyy-MM-dd
HH:mm:ss");
break;
case
"2":
msg.Content
=
Tools.HttpCrossDomain.Get("/data/sk/101210301.html");
break;
case
"3":
msg.Content
=
"來(lái)吧,想嘮點(diǎn)什么呢?
[偷笑]";
break;
case
"你是男的女的":
msg.Content
=
"姑娘一枚
[偷笑]";
break;
default:
msg.Content
=
"抱歉哦,我不認(rèn)識(shí)您輸入的命令。/害羞
/:,@-D";
break;
}
return
XmlEntityExchange<WxTextMsg>.ConvertEntity2Xml(msg);
}
///
<summary>
///
獲取幫助菜單文檔
///
</summary>
///
<returns></returns>
private
string
HelpDocument()
{
StringBuilder
sb
=
new
StringBuilder();
sb.Append("HI,請(qǐng)回復(fù)數(shù)字,選擇服務(wù)").Append("\n\n");
sb.Append("1.
當(dāng)前時(shí)間").Append("\n");
sb.Append("2.
天氣信息").Append("\n");
sb.Append("3.
聊天嘮嗑").Append("\n");
sb.Append("n.
更多服務(wù)開發(fā)中...").Append("\n");
sb.Append("回復(fù)【?】顯示此幫助菜單");
return
sb.ToString();
}
///
<summary>
///
當(dāng)用戶關(guān)注微信帳號(hào)的時(shí)候觸發(fā),事件響應(yīng)
///
</summary>
///
<param
name="xml"></param>
private
string
OnAttention(string
xml)
{
WxEvent
wxevent
=
XmlEntityExchange<WxEvent>.ConvertXml2Entity(xml);
WxTextMsg
msg
=
new
WxTextMsg();
msg.ToUserName
=
wxevent.FromUserName;
msg.FromUserName
=
wxevent.ToUserName;
msg.CreateTime
=
DateTime.Now.Ticks;
msg.MsgType
=
"text";
//如果是關(guān)注,則發(fā)送歡迎消息
switch
(wxevent.Event)
{
case
"subscribe":
msg.Content
=
System.Configuration.ConfigurationManager.AppSettings["DefaultWxMsg"];
break;
case
"CLICK":
msg.Content
=
"您單擊的是:"
+
wxevent.EventKey;
break;
default:
msg.Content
=
"暫未處理的事件:Event"
+
wxevent.Event
+
";EventKey:"
+
wxevent.EventKey;
break;
}
string
rst
=
XmlEntityExchange<WxTextMsg>.ConvertEntity2Xml(msg);
log.Info(rst);
return
rst;
}
}
}//實(shí)體
public
class
WxEvent
{
///
<summary>
///
接收人
///
</summary>
public
string
ToUserName
{
get;
set;
}
///
<summary>
///
發(fā)送人
///
</summary>
public
string
FromUserName
{
get;
set;
}
///
<summary>
///
時(shí)間
///
</summary>
public
string
CreateTime
{
get;
set;
}
///
<summary>
///
類型
///
</summary>
public
string
MsgType
{
get;
set;
}
///
<summary>
///
事件
///
</summary>
public
string
Event
{
get;
set;
}
public
string
EventKey
{
get;
set;
}
}
public
class
WxTextMsg
{
///
<summary>
///
接收人
///
</summary>
public
string
ToUserName
{
get;
set;
}
///
<summary>
///
發(fā)送人
///
</summary>
public
string
FromUserName
{
get;
set;
}
///
<summary>
///
時(shí)間
///
</summary>
public
long
CreateTime
{
get;
set;
}
///
<summary>
///
類型
///
</summary>
public
string
MsgType
{
get;
set;
}
///
<summary>
///
內(nèi)容
///
</summary>
public
string
Content
{
get;
set;
}
}//XML和實(shí)體對(duì)象間的相互轉(zhuǎn)化
namespace
ElegantWM.WebUI.Areas.WeiXin
{
public
class
XmlEntityExchange<T>
where
T
:
new()
{
///
<summary>
///
將XML轉(zhuǎn)換為對(duì)象
///
</summary>
///
<param
name="xml"></param>
///
<returns></returns>
public
static
T
ConvertXml2Entity(string
xml)
{
XmlDocument
doc
=
new
XmlDocument();
PropertyInfo[]
propinfos
=
null;
doc.LoadXml(xml);
XmlNodeList
nodelist
=
doc.SelectNodes("/xml");
T
entity
=
new
T();
foreach
(XmlNode
node
in
nodelist)
{
//初始化propertyinfo
if
(propinfos
==
null)
{
Type
objtype
=
entity.GetType();
propinfos
=
objtype.GetProperties();
}
//填充entity類的屬性
foreach
(PropertyInfo
pi
in
propinfos)
{
XmlNode
cnode
=
node.SelectSingleNode(pi.Name);
pi.SetValue(entity,
Convert.ChangeType(cnode.InnerText,
pi.PropertyType),
null);
}
}
return
entity;
}
///
<summary>
///
構(gòu)造微信消息
///
</summary>
///
<param
name="t">對(duì)象實(shí)體</param>
///
<returns>返回微信消息xml格式</returns>
public
static
string
ConvertEntity2Xml(T
t)
{
StringBuilder
builder
=
new
StringBuilder();
builder.Append("<xml>");
Type
objtype
=
t.GetType();
//填充entity類的屬性
foreach
(PropertyInfo
pi
in
objtype.GetProperties())
{
object
obj
=
pi.GetValue(t);
string
value
=
obj
==
null
?
""
:
obj.ToString();
if
(pi.PropertyType.Name.ToLower()
==
"int64")
builder.Append("<"
+
pi.Name
+
">"
+
value
+
"</"
+
pi.Name
+
">");
else
builder.Append("<"
+
pi.Name
+
"><![CDATA["
+
value
+
"]]></"
+
pi.Name
+
">");
}
builder.Append("</xml>");
return
builder.ToString();
}
}
}/upload/information/20201208/260/14950.jpg/*
*
*
Copyright
?
2013
CCT
All
Rights
Reserved
*
作者:JackChain
*
時(shí)間:2013/8/23
18:21:23
*
功能:跨域訪問(wèn)
*
版本:V1.0
*
*
修改人:
*
修改點(diǎn):
*
*/namespace
ElegantWM.Tools
{
public
class
HttpCrossDomain
{
///
<summary>
///
跨域訪問(wèn)
///
</summary>
///
<param
name="url"></param>
///
<param
name="param"></param>
///
<returns></returns>
public
static
string
Post(string
url,
string
param,
int
time
=
60000)
{
Uri
address
=
new
Uri(url);
HttpWebRequest
request
=
WebRequest.Create(address)
as
HttpWebRequest;
request.Method
=
"POST";
request.ContentType
=
"application/json;charset=utf-8";
//"application/x-www-form-urlencoded";
request.Timeout
=
time;
byte[]
byteData
=
UTF8Encoding.UTF8.GetBytes(param
==
null
?
""
:
param);
request.ContentLength
=
byteData.Length;
using
(Stream
postStream
=
request.GetRequestStream())
{
postStream.Write(byteData,
0,
byteData.Length);
}
string
result
=
"";
using
(HttpWebResponse
response
=
request.GetResponse()
as
HttpWebResponse)
{
StreamReader
reader
=
new
StreamReader(response.GetResponseStream());
result
=
reader.ReadToEnd();
}
return
(result);
}
///
<summary>
///
跨域訪問(wèn)
///
</summary>
///
<param
name="url"></param>
///
<param
name="param"></param>
///
<returns></returns>
public
static
string
Get(string
url,
int
time
=
60000)
{
Uri
address
=
new
Uri(url);
HttpWebRequest
request
=
WebRequest.Create(address)
as
HttpWebRequest;
request.Method
=
"GET";
request.ContentType
=
"application/json;charset=utf-8";
//"application/x-www-form-urlencoded";
request.Timeout
=
time;
string
result
=
"";
using
(HttpWebResponse
response
=
request.GetResponse()
as
HttpWebResponse)
{
StreamReader
reader
=
new
StreamReader(response.GetResponseStream());
result
=
reader.ReadToEnd();
}
return
(result);
}
}
}public
class
CommonController
:
Controller
{
///
<summary>
///
獲取微信憑證
///
</summary>
///
<returns></returns>
public
JsonResult
GetWxCredential()
{
string
url
=
"/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}";
url
=
string.Format(url,
ConfigurationManager.AppSettings["AppId"],
ConfigurationManager.AppSettings["AppSecret"]);
string
rst
=
HttpCrossDomain.Get(url);
if
(rst.Contains("access_token"))
{
string
tokenId=rst.Replace("{\"access_token\":\"",
"").Replace("\",\"expires_in\":7200}",
"");
CacheHelper.CacheInsertAddMinutes("
溫馨提示
- 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ù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025版建筑材料購(gòu)銷合同書模板
- 二零二五年度臺(tái)球室租賃及品牌形象合作合同3篇
- 2025購(gòu)銷合同常用文本
- 二零二五年度全新租賃房屋合同住宅押金退還管理協(xié)議3篇
- 2025年度全新出售房屋買賣貸款擔(dān)保合同3篇
- 2025年度年度全新高空纜車運(yùn)營(yíng)意外事故免責(zé)服務(wù)協(xié)議3篇
- 二零二五年度智慧社區(qū)建設(shè)與運(yùn)營(yíng)管理協(xié)議合同范文2篇
- 2025年農(nóng)村兄弟分家協(xié)議及遺產(chǎn)分配執(zhí)行方案
- 2025年度養(yǎng)殖場(chǎng)勞務(wù)合同(養(yǎng)殖場(chǎng)安全生產(chǎn)監(jiān)管)3篇
- 二零二五年度創(chuàng)業(yè)投資股權(quán)代持專項(xiàng)合同2篇
- 漢語(yǔ)教程我聽過(guò)鋼琴協(xié)奏曲黃河課件
- 二氧化碳充裝流程
- 12m跨鋼棧橋設(shè)計(jì)計(jì)算
- 電路板類英語(yǔ)詞匯
- DES算法Matlab代碼
- 沙特的礦產(chǎn)資源開發(fā)概況及其商機(jī)
- 高一生物必修一期末試題(附答案)
- 安全事故應(yīng)急響應(yīng)程序流程圖(共1頁(yè))
- 三年級(jí)_上冊(cè)牛津英語(yǔ)期末試卷
- 損傷容限設(shè)計(jì)基本概念原理和方法PPT課件
- 水壓式沼氣池設(shè)計(jì)
評(píng)論
0/150
提交評(píng)論