asp.net使用微信发布模版消息核心代码
来源:互联网 发布:mac虚拟机共享文件 编辑:程序博客网 时间:2024/05/21 13:21
自己在微信 功能 - 添加功能插件 - 添加模块消息
自己定义 WxPayConfig类,用于调用微信的appid 等
1、获取access_token
public string GetAccess_Token()
{
string appid = WxPayConfig.APPID;
string appsecret = WxPayConfig.APPSECRET;
string access_token = "";
string token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + appsecret;
string result = HttpService.Get(token_url);
JsonData jd = JsonMapper.ToObject(result);
access_token = (string)jd["access_token"];
return access_token;
}
2、模版消息使用Post 推送
public string ModelMessageSend(string data)
{
string url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + GetAccess_Token();
string result = HttpPost(url,data);
if (result.Contains("ok"))
{
return "OK";
}
else
{
return "消息推送失败,具体错误为:"+result;
}
}
3、HttpPost方法
public string HttpPost(string url,string postData)
{
byte[] data = Encoding.UTF8.GetBytes(postData);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "Post";
request.ContentType = "application/json";
request.ContentLength = data.Length;
request.KeepAlive = true;
Stream stream = request.GetRequestStream();
stream.Write(data, 0, data.Length);
HttpWebResponse response;
try
{
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
response = (HttpWebResponse)ex.Response;
}
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string content = reader.ReadToEnd();
request.Abort();
response.Close();
reader.Dispose();
stream.Close();
stream.Dispose();
return content;
}
4、调用进行消息发送
ModelMessageSend(“此处为你的模版/在微信获取”);
例如:会员充值模版
{\"touser\":\"" + wxopenid + "\",\"template_id\":\"0YeXNaDr9KkmF55WdRj9_tRiRCjpV6x8eqTNR0FKNf8\",\"url\":\"\",\"topcolor\":\"#FF0000\",\"data\":{\"first\": {\"value\":\"" + strxm + ",您已成功充值\",\"color\":\"#000000\"},\"accountType\":{\"value\":\"会员卡号\",\"color\":\"#000000\"},\"account\":{\"value\":\"" + touser + "\",\"color\":\"#000000\"},\"amount\":{\"value\":\"" + je + "\",\"color\":\"#000000\"},\"result\":{\"value\":\"充值成功\",\"color\":\"#000000\"},\"remark\":{\"value\":\"" + qtnr + "\",\"color\":\"#000000\"}}}
里面的函数是我定义的。 可以更换为自己的。
- asp.net使用微信发布模版消息核心代码
- 微信 模版消息 (六)
- 微信模版消息开发
- 微信模版消息+ThinkPHP
- 微信推送模版消息
- asp.net 微信支付 实例代码
- 微信公众平台模版消息
- 微信公众平台开发 模版消息
- 微信公众平台 模版消息
- 微信自定义推送模版消息
- 微信发送消息 (模版与接口)
- [ASP.net]DataGrid自定义模版前端代码
- ASP.NET微信公众帐号消息帐号接入指南
- 微信分享核心代码
- C# asp.net 搭建微信公众平台(可实现关注消息与消息自动回复)的代码以及我所遇到的问题
- asp.net微信公众平台开发(3)微信消息封装及反射赋值
- 【模版消息】C#推送微信模版消息(Senparc.Weixin.MP.dll)
- 使用delphi+intraweb进行微信开发2—intraweb以.net方式发布(以asp.net mvc4模式部署)在IIS(.net虚拟主机)上
- 关于Linux配置django环境
- Java中final、static关键字使用总结
- sessionStorage 、localStorage 和 cookie区别及使用
- 关于在线安装vs2017缺少头文件的解决办法
- Thinkphp的where条件
- asp.net使用微信发布模版消息核心代码
- 在Azure上部署微服务项目的经验分享
- linux不同类型包的安装
- RUBER: An Unsupervised Method for Automatic Evaluation of Open-Domain Dialog Systems
- 关于MySQL 1045等错误问题
- angular 路由跳转读取参数
- 最易理解的自动拆箱和装箱
- fusionCharts——拆线图
- Leetcode--268. Missing Number