欢迎来到入门教程网!

C#教程

当前位置:主页 > 软件编程 > C#教程 >

C#微信接口之推送模板消息功能示例

来源:本站原创|时间:2020-01-10|栏目:C#教程|点击:

本文实例讲述了C#微信接口之推送模板消息功能。分享给大家供大家参考,具体如下:

public string SendTempletMessge()
{
   string strReturn = string.Empty;
   try
   {
     #region 获取access_token
     string apiurl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=你的appid&secret=你的secret";
     WebRequest request = WebRequest.Create(@apiurl);
     request.Method = "POST";
     WebResponse response = request.GetResponse();
     Stream stream = response.GetResponseStream();
     Encoding encode = Encoding.UTF8;
     StreamReader reader = new StreamReader(stream, encode);
     string detail = reader.ReadToEnd();
     var jd = JsonConvert.DeserializeObject<WXApi>(detail);
     string token = (String)jd.access_token;
     #endregion
     #region 组装信息推送,并返回结果(其它模版消息于此类似)
     string url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + token;
     string temp = "{\"touser\": \"" + UserInfo.OpenID + "\"," +
            "\"template_id\": \"f3kRRjJeyLDf4tndtg-OJeRvgEdgjjDxCy4T9kuwM70\", " +
            "\"topcolor\": \"#FF0000\", " +
            "\"data\": " +
            "{\"first\": {\"value\": \"您好,您有一条回款通知信息\"}," +
            "\"keyword1\": { \"value\": \"单位名称\"}," +
            "\"keyword2\": { \"value\": \"日期\"}," +
            "\"keyword3\": { \"value\": \"金额\"}," +
            "\"keyword4\": { \"value\": \"业务员\"}," +
            "\"remark\": {\"value\": \"\" }}}";
     #endregion
   //核心代码
   GetResponseData(temp, @url);
     strReturn = "推送成功";
   }
   catch (Exception ex)
   {
    strReturn = ex.Message;
   }
   return strReturn;
}
/// <summary>
/// 返回JSon数据
/// </summary>
/// <param name="JSONData">要处理的JSON数据</param>
/// <param name="Url">要提交的URL</param>
/// <returns>返回的JSON处理字符串</returns>
public string GetResponseData(string JSONData, string Url)
{
   byte[] bytes = Encoding.UTF8.GetBytes(JSONData);
   HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
   request.Method = "POST";
   request.ContentLength = bytes.Length;
   request.ContentType = "json";
   Stream reqstream = request.GetRequestStream();
   reqstream.Write(bytes, 0, bytes.Length);
   //声明一个HttpWebRequest请求
   request.Timeout = 90000;
   //设置连接超时时间
   request.Headers.Set("Pragma", "no-cache");
   HttpWebResponse response = (HttpWebResponse)request.GetResponse();
   Stream streamReceive = response.GetResponseStream();
   Encoding encoding = Encoding.UTF8;
   StreamReader streamReader = new StreamReader(streamReceive, encoding);
   string strResult = streamReader.ReadToEnd();
   streamReceive.Dispose();
   streamReader.Dispose();
   return strResult;
}

涉及到的实体:

public class WXApi
{
  public string access_token { set; get; }
}

更多关于C#相关内容感兴趣的读者可查看本站专题:《C#常见控件用法教程》、《WinForm控件用法总结》、《C#数据结构与算法教程》、《C#面向对象程序设计入门教程》及《C#程序设计之线程使用技巧总结》

希望本文所述对大家C#程序设计有所帮助。

上一篇:C#多线程ThreadPool线程池详解

栏    目:C#教程

下一篇:C#实现获取mp3 Tag信息的方法

本文标题:C#微信接口之推送模板消息功能示例

本文地址:https://www.xiuzhanwang.com/a1/C_jiaocheng/5597.html

网页制作CMS教程网络编程软件编程脚本语言数据库服务器

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:835971066 | 邮箱:835971066#qq.com(#换成@)

Copyright © 2002-2020 脚本教程网 版权所有