资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

微信开发之微信发送消息的示例分析-创新互联

这篇文章主要为大家展示了“微信开发之微信发送消息的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“微信开发之微信发送消息的示例分析”这篇文章吧。

创新互联建站专注于网站建设|成都网站维护|优化|托管以及网络推广,积累了大量的网站设计与制作经验,为许多企业提供了网站定制设计服务,案例作品覆盖护栏打桩机等行业。能根据企业所处的行业与销售的产品,结合品牌形象的塑造,量身建设品质网站。

1,首先,获取开发者测试账号(申请),会根据当前扫码提供的账号生成测试账号: 链接地址:http://mp.weixin.qq.com/wiki/home/index.html

微信开发之微信发送消息的示例分析

这时候可以获取到测试用的appid和appsecrept,然后调用获取接口调用凭证 接口获取access_token;

2,下面说信息发送,模拟了单用户信息发送和多用户消息批量发送

(1)基础方法,http方法    

/// 
        ///      http  get/post 公用方法
        /// 
        /// 请求链接
        /// 请求参数值(如果是get方式此处为“”值,默认为 "")
        /// 请求方式  post or get
        /// 
        public static string Request(this string requestUrl, string requestMethod, string requestJsonParams = "")
        {
            string returnText = "";
            StreamReader streamReader = null;
            HttpWebRequest request = null;
            HttpWebResponse response = null;

            Encoding encoding = Encoding.UTF8;
            request = (HttpWebRequest)WebRequest.Create(requestUrl);
            request.Method = requestMethod;
            if (!string.IsNullOrEmpty(requestJsonParams) && requestMethod.ToLower() == "post")
            {
                byte[] buffer = encoding.GetBytes(requestJsonParams);
                request.ContentLength = buffer.Length;
                request.GetRequestStream().Write(buffer, 0, buffer.Length);
            }

            try
            {
                response = (HttpWebResponse)request.GetResponse();
                using (streamReader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding("gb2312")))//utf-8
                {
                    returnText = streamReader.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                returnText = ex.Message;
            }

            return returnText;
        }

(2)模拟发送:  

/// 
        ///     发送微信信息(单用户发送)
        /// 
        /// 授权码(微信token)
        /// 发送信息模型
        /// 
        public static string SendSingleMessage(WeChatParamEntity messageInfo, string access_token)
        {
            messageInfo.MsgType = string.IsNullOrEmpty(messageInfo.MsgType) ? "text" : messageInfo.MsgType;
            string jsonDataParams = messageInfo == null ? "" : JsonConvert.SerializeObject(new
            {
                touser = messageInfo.ToUser,
                msgtype = messageInfo.MsgType,
                text = new { content = messageInfo.Text }
            });
            string requestUrl = string.Format(Consts.URL_POSTSINGLETEXTMESSAGE, access_token);
            return requestUrl.Request("POST", jsonDataParams);
        }
        /// 
        ///     发送微信信息(多用户批量发送)
        /// 
        /// 授权码(微信token)
        /// 发送信息模型
        /// 
        public static string SendMessages(WeChatParamsEntity messageInfo, string access_token)
        {
            messageInfo.MsgType = string.IsNullOrEmpty(messageInfo.MsgType) ? "text" : messageInfo.MsgType;
            string jsonDataParams = messageInfo == null ? "" : JsonConvert.SerializeObject(new
            {
                touser = messageInfo.ToUser,
                msgtype = messageInfo.MsgType,
                text = new { content = messageInfo.Text }
            });
            string requestUrl = string.Format(Consts.URL_POSTTEXTMESSAGES, access_token);
            return requestUrl.Request("POST", jsonDataParams);
        }

(3)两个参数 模型: 

/// 
    ///     微信 发送信息 参数实体模型
    /// 
    public class WeChatParamEntity
    {
        /// 
        ///     普通用户openid
        /// 
        public string ToUser { get; set; }
        /// 
        ///     传输的文件类型(text,image, and so on)
        /// 
        public string MsgType { get; set; } = "text";
        /// 
        ///     传输文本内容
        /// 
        public string Text { get; set; }

    }

    /// 
    ///     微信 发送信息 参数实体模型
    /// 
    public class WeChatParamsEntity
    {
        /// 
        ///     普通用户openid
        /// 
        public string[] ToUser { get; set; }
        /// 
        ///     传输的文件类型(text,image, and so on)
        /// 
        public string MsgType { get; set; } = "text";
        /// 
        ///     传输文本内容
        /// 
        public string Text { get; set; }

    }

(4)web.config中的链接



    
    
    
    
    
    
    
    

3,测试使用涉及到  touser的这个参数,这个是需要发送的对象的  openID,这个很简单,在开发者文档(也就是上面的步骤二中,)获取

appid  和appsecrept的时候,当前这个页面下面有一个二维码,找几个人用微信扫扫就可以自动获取openID ,这时候将参数带入脚本模拟

post即可

另外需要注意:文档中提示的  json 参数格式

注意三:token有效时间为7200,俩小时,需要判断当前发送信息用户的token有效性,同时每天较大可请求次数为2000.

获取token :

#region 获取token,并验证token过期时间

        public static string GetAccessToken(string appid, string appSecret)
        {
            string token = "";
            string requestUrl = string.Format(ConfigBLL.URL_GETACCESSTOKEN, appid, appSecret);
            string requestResult = WebAPITransfer.Request(requestUrl, "GET", "");

            CommonBLL.DebugLog(requestResult, "AccessToken-token-Params");
            string[] strArray = requestResult.Split(',');
            token = strArray[0].Split(':')[1].Replace("\"", "");

            return token;
        }

        #endregion

以上是“微信开发之微信发送消息的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!


网站名称:微信开发之微信发送消息的示例分析-创新互联
网页URL:http://cdkjz.cn/article/dgjoes.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220