zoukankan      html  css  js  c++  java
  • [微信小程序]向指定用户推送模版消息(c#代码)亲测可用

    调用:SubscribeMessageToUser("用户openid字符串");
    官方比较坑:限制必须点击授权按钮的时候才能执行否则就会提示:
    {"errcode":43101,"errmsg":"user refuse to accept the msg rid: 5f66d001-785045b2-5443a9f9"}
    翻译解释为:用户拒绝接收信息

    小程序向指定用户推送模版消息(c#代码)      [HttpPost] public JsonResult SubscribeMessageToUser(string openid, string title=null, string content=null) { AjaxResult res = new AjaxResult(); res.state = false; try { AccessTokenModel AccessTokenModel = xcx_Get_Token();//获取access_token string _url = string.Format("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token={0}", AccessTokenModel.access_token); //json参数 string jsonParam = Newtonsoft.Json.JsonConvert.SerializeObject(new { touser = openid,//用戶openid template_id = "模版ID", // 模版template_id page = "pages/index/index", //miniprogram_state = "developer", lang = "zh_CN", data = new { thing1 = new { value = "签到提醒" }, time5 = new { value = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") }, thing2 = new { value = "点击签到" } } }); var request = (HttpWebRequest)WebRequest.Create(_url); request.Method = "POST"; request.ContentType = "application/json;charset=UTF-8"; byte[] byteData = Encoding.UTF8.GetBytes(jsonParam); int length = byteData.Length; request.ContentLength = length; Stream writer = request.GetRequestStream(); writer.Write(byteData, 0, length); writer.Close(); var response = (HttpWebResponse)request.GetResponse(); var responseString = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")).ReadToEnd(); JavaScriptSerializer js = new JavaScriptSerializer(); //实例化一个能够序列化数据的类 AccessTokenModel_Err ErrInfo = js.Deserialize<AccessTokenModel_Err>(responseString); //AccessTokenModel_Err ErrInfo = (AccessTokenModel_Err)JsonConvert.DeserializeObject(responseString); res.message = ErrInfo.errmsg; if(ErrInfo.errcode==0) res.state = true; //return responseString.ToString(); } catch (Exception ex) { res.state = false; res.message = ex.Message; } return Json(res, JsonRequestBehavior.AllowGet); }

      

  • 相关阅读:
    爬取校园新闻首页的新闻的详情,使用正则表达式,函数抽离
    网络爬虫基础练习
    Mysql 使用 select into outfile
    Mysql 使用CMD 登陆
    使用Clean() 去掉由函数自动生成的字符串中的双引号
    Get Resultset from Oracle Stored procedure
    获取引用某个主键的所有外键的表
    Entity Framework 丢失数据链接的绑定,在已绑好的EDMX中提示“Choose Your Data Connection”
    添加MySql Metat Database 信息
    at System.Data.EntityClient.EntityConnection.GetFactory(String providerString)
  • 原文地址:https://www.cnblogs.com/romanticcrystal/p/13699290.html
Copyright © 2011-2022 走看看