版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】C#中.net微信开发的示例分析
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>
///
微信公共服务类
///
</summary>
public
class
RobotController
:
BaseController
{
///
<summary>
///
接收微信请求接口,认证的接口
///
</summary>
///
<returns></returns>
public
ContentResult
Index()
{
string
result
=
AnalyzeXmlFromWeiXin();
return
Content(result);
}
///
<summary>
///
分析微信POST到本服务器的XML数据
///
</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
"不被支持的关键字!";
}
}
///
<summary>
///
响应文本消息
///
</summary>
///
<param
name="xml"></param>
///
<returns></returns>
private
string
OnReceiveTextMsg(string
xml)
{
WxTextMsg
msg
=
XmlEntityExchange<WxTextMsg>.ConvertXml2Entity(xml);
//返回消息,互换收发用户
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
=
"来吧,想唠点什么呢?
[偷笑]";
break;
case
"你是男的女的":
msg.Content
=
"姑娘一枚
[偷笑]";
break;
default:
msg.Content
=
"抱歉哦,我不认识您输入的命令。/害羞
/:,@-D";
break;
}
return
XmlEntityExchange<WxTextMsg>.ConvertEntity2Xml(msg);
}
///
<summary>
///
获取帮助菜单文档
///
</summary>
///
<returns></returns>
private
string
HelpDocument()
{
StringBuilder
sb
=
new
StringBuilder();
sb.Append("HI,请回复数字,选择服务").Append("\n\n");
sb.Append("1.
当前时间").Append("\n");
sb.Append("2.
天气信息").Append("\n");
sb.Append("3.
聊天唠嗑").Append("\n");
sb.Append("n.
更多服务开发中...").Append("\n");
sb.Append("回复【?】显示此帮助菜单");
return
sb.ToString();
}
///
<summary>
///
当用户关注微信帐号的时候触发,事件响应
///
</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";
//如果是关注,则发送欢迎消息
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;
}
}
}//实体
public
class
WxEvent
{
///
<summary>
///
接收人
///
</summary>
public
string
ToUserName
{
get;
set;
}
///
<summary>
///
发送人
///
</summary>
public
string
FromUserName
{
get;
set;
}
///
<summary>
///
时间
///
</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>
///
发送人
///
</summary>
public
string
FromUserName
{
get;
set;
}
///
<summary>
///
时间
///
</summary>
public
long
CreateTime
{
get;
set;
}
///
<summary>
///
类型
///
</summary>
public
string
MsgType
{
get;
set;
}
///
<summary>
///
内容
///
</summary>
public
string
Content
{
get;
set;
}
}//XML和实体对象间的相互转化
namespace
ElegantWM.WebUI.Areas.WeiXin
{
public
class
XmlEntityExchange<T>
where
T
:
new()
{
///
<summary>
///
将XML转换为对象
///
</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>
///
构造微信消息
///
</summary>
///
<param
name="t">对象实体</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
*
时间:2013/8/23
18:21:23
*
功能:跨域访问
*
版本:V1.0
*
*
修改人:
*
修改点:
*
*/namespace
ElegantWM.Tools
{
public
class
HttpCrossDomain
{
///
<summary>
///
跨域访问
///
</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>
///
跨域访问
///
</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. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2023年国家公务员考试《申论》真题(副省卷)及答案解析
- 《制度经济学》课程教学大纲
- 河北省衡水市重点高中2024-2025学年高一上学期10月期中考试化学试题含答案
- 2024年伐木出售合同范本
- 2024年代收蔬菜合同范本
- 2024年承接装裱加工合同范本
- 园本课程理论培训
- 安徽省池州市2024-2025学年九年级上学期期中道德与法治试题(含答案)
- 侧压充填技术护理配合
- 初级老年护理员培训
- 东方绿洲军训日记500字(八篇)
- 中心静脉压测量技术-中华护理学会团体标准2023
- 原发性骨质疏松症诊疗指南(2022版)第二部分
- 初中英语课外阅读Treasure+Island黑布林阅读
- 临床医学概论题库(含答案)
- 急救物品检查表
- 屋面融雪系统施工方案
- 医学院《急诊医学》教学大纲
- 巯基乙醇化学品安全技术说明书
- 小学道德与法治课评分表
- 汽修厂搞个优惠活动
评论
0/150
提交评论