查看: 3640|回复: 0

[ASP.NET教程] C#微信接口之推送模板消息功能示例

发表于 2018-2-10 08:00:07

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

  1. public string SendTempletMessge()
  2. {
  3. string strReturn = string.Empty;
  4. try
  5. {
  6. #region 获取access_token
  7. string apiurl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=你的appid&secret=你的secret";
  8. WebRequest request = WebRequest.Create(@apiurl);
  9. request.Method = "POST";
  10. WebResponse response = request.GetResponse();
  11. Stream stream = response.GetResponseStream();
  12. Encoding encode = Encoding.UTF8;
  13. StreamReader reader = new StreamReader(stream, encode);
  14. string detail = reader.ReadToEnd();
  15. var jd = JsonConvert.DeserializeObject<WXApi>(detail);
  16. string token = (String)jd.access_token;
  17. #endregion
  18. #region 组装信息推送,并返回结果(其它模版消息于此类似)
  19. string url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + token;
  20. string temp = "{\"touser\": \"" + UserInfo.OpenID + "\"," +
  21. "\"template_id\": \"f3kRRjJeyLDf4tndtg-OJeRvgEdgjjDxCy4T9kuwM70\", " +
  22. "\"topcolor\": \"#FF0000\", " +
  23. "\"data\": " +
  24. "{\"first\": {\"value\": \"您好,您有一条回款通知信息\"}," +
  25. "\"keyword1\": { \"value\": \"单位名称\"}," +
  26. "\"keyword2\": { \"value\": \"日期\"}," +
  27. "\"keyword3\": { \"value\": \"金额\"}," +
  28. "\"keyword4\": { \"value\": \"业务员\"}," +
  29. "\"remark\": {\"value\": \"\" }}}";
  30. #endregion
  31. //核心代码
  32. GetResponseData(temp, @url);
  33. strReturn = "推送成功";
  34. }
  35. catch (Exception ex)
  36. {
  37. strReturn = ex.Message;
  38. }
  39. return strReturn;
  40. }
  41. /// <summary>
  42. /// 返回JSon数据
  43. /// </summary>
  44. /// <param name="JSONData">要处理的JSON数据</param>
  45. /// <param name="Url">要提交的URL</param>
  46. /// <returns>返回的JSON处理字符串</returns>
  47. public string GetResponseData(string JSONData, string Url)
  48. {
  49. byte[] bytes = Encoding.UTF8.GetBytes(JSONData);
  50. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
  51. request.Method = "POST";
  52. request.ContentLength = bytes.Length;
  53. request.ContentType = "json";
  54. Stream reqstream = request.GetRequestStream();
  55. reqstream.Write(bytes, 0, bytes.Length);
  56. //声明一个HttpWebRequest请求
  57. request.Timeout = 90000;
  58. //设置连接超时时间
  59. request.Headers.Set("Pragma", "no-cache");
  60. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  61. Stream streamReceive = response.GetResponseStream();
  62. Encoding encoding = Encoding.UTF8;
  63. StreamReader streamReader = new StreamReader(streamReceive, encoding);
  64. string strResult = streamReader.ReadToEnd();
  65. streamReceive.Dispose();
  66. streamReader.Dispose();
  67. return strResult;
  68. }
复制代码

涉及到的实体:

  1. public class WXApi
  2. {
  3. public string access_token { set; get; }
  4. }
复制代码

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

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

您可能感兴趣的文章:

  • c#使用微信接口开发微信门户应用
  • c#使用微信接口开发微信门户应用中微信消息的处理和应答
  • C#开发微信公众号接口开发
  • C#微信公众平台开发之高级群发接口
  • C#.net 微信公众账号接口开发
  • C#微信公众号与订阅号接口开发示例代码
  • C#微信开发之获取接口调用凭据
  • C#开发微信门户及应用(1) 微信接口使用
  • C#微信开发之发送模板消息
  • C#调用微信接口的相关代码


回复

使用道具 举报