微信开发如何实现回复用户消息功能
这篇文章主要介绍微信开发如何实现回复用户消息功能,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
创新互联拥有10多年成都网站建设工作经验,为各大企业提供网站建设、网站设计服务,对于网页设计、PC网站建设(电脑版网站建设)、App定制开发、wap网站建设(手机版网站建设)、程序开发、网站优化(SEO优化)、微网站、空间域名等,凭借多年来在互联网的打拼,我们在互联网网站建设行业积累了很多网站制作、网站设计、网络营销经验,集策划、开发、设计、营销、管理等网站化运作于一体,具备承接各种规模类型的网站建设项目的能力。
示例
/// /// 要发送的人ID /// 消息 private void SendMessage(string UserID, string StrMessg) { Como.LogUtil.WriteLog("回复用户" + UserID + "消息"); string Access_Token = Como.GetAccessToken.GetAccess_token(); if (Access_Token == "") Como.LogUtil.WriteException("SendMessage 未能成功加载Access_Token"); string Text = @"{ ""touser"":"; Text += '"' + UserID + '"'; Text += "," + '"' + @"msgtype"": ""text"", ""agentid"": ""5"", ""text"": { ""content"":"; Text += '"' + StrMessg + '"'; Text += @"}, ""safe"": ""0"" }"; ; string url = String.Format("https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={0}", Access_Token); string strResult = Como.Tools.GetPage(url, Text); JArray ja = (JArray)JsonConvert.DeserializeObject("[" + strResult + "]"); string Error = ""; try { if (strResult.Contains("errcode")) { Error = ja[0]["errcode"].ToString(); } } catch (Exception ex) { Como.LogUtil.WriteException("获取strResult,未获取到错误信息" + ex.Message.ToString()); } string errcode = ja[0]["errcode"].ToString(); string errmsg = ja[0]["errmsg"].ToString(); if (errcode == "0" && errmsg == "ok") { Como.LogUtil.WriteLog("回复成功!"); } else { Como.LogUtil.WriteLog("回复失败!"); Como.LogUtil.WriteException("回复失败:SendMessage:" + strResult); } }
////// 根据请求地址 和数据返回请求结果 /// /// /// ///public static string GetPage(string posturl, string postData) { Stream outstream = null; Stream instream = null; StreamReader sr = null; HttpWebResponse response = null; HttpWebRequest request = null; Encoding encoding = Encoding.UTF8; byte[] data = encoding.GetBytes(postData); // 准备请求... try { // 设置参数 request = WebRequest.Create(posturl) as HttpWebRequest; CookieContainer cookieContainer = new CookieContainer(); request.CookieContainer = cookieContainer; request.AllowAutoRedirect = true; request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = data.Length; outstream = request.GetRequestStream(); outstream.Write(data, 0, data.Length); outstream.Close(); //发送请求并获取相应回应数据 response = request.GetResponse() as HttpWebResponse; //直到request.GetResponse()程序才开始向目标网页发送Post请求 instream = response.GetResponseStream(); sr = new StreamReader(instream, encoding); //返回结果网页(html)代码 string content = sr.ReadToEnd(); string err = string.Empty; return content; } catch (Exception ex) { string err = ex.Message; Como.LogUtil.WriteException("Tools GetPage 异常为:"+err.ToString()); return string.Empty; } }
参数引用详解:
1.Como.GetAccessToken.GetAccess_token() //获取 Access_token
2.GetPage //返回页面请求信息 结果
以上为基础发送关键代码:
以下为:成功后的实例图:
以上是“微信开发如何实现回复用户消息功能”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!
分享名称:微信开发如何实现回复用户消息功能
本文路径:http://azwzsj.com/article/gipies.html