asp.net微信开发(高级群发图文)(3)

/// <summary> /// 预览图文消息 /// </summary> /// <param></param> /// <param></param> protected void LinkBtnSendPreview_Click(object sender, EventArgs e) { Session["media_id"] = null; //非空验证 if (String.IsNullOrWhiteSpace(this.txttuwen_title.Value.ToString())) { ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('请输入图文标题!');", true); this.txttuwen_title.Focus(); return; } if (this.ImgTuWen2.ImageUrl.ToString().Equals("")) { ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('必须上传一张图片!');", true); this.ImgTuWen2.Focus(); return; } if (String.IsNullOrWhiteSpace(this.tbContent.InnerText.ToString())) { ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('请输入正文内容!');", true); this.tbContent.Focus(); return; } //对各项进行赋值 WeiXinServer wxs = new WeiXinServer(); ///从缓存读取accesstoken string Access_token = Cache["Access_token"] as string; if (Access_token == null) { //如果为空,重新获取 Access_token = wxs.GetAccessToken(); //设置缓存的数据7000秒后过期 Cache.Insert("Access_token", Access_token, null, DateTime.Now.AddSeconds(7000), System.Web.Caching.Cache.NoSlidingExpiration); } string Access_tokento = Access_token.Substring(17, Access_token.Length - 37); //POST数据例子: POST数据例子: //{ // "articles": [{ // "title": TITLE, // "thumb_media_id": THUMB_MEDIA_ID, // "author": AUTHOR, // "digest": DIGEST, // "show_cover_pic": SHOW_COVER_PIC(0 / 1), // "content": CONTENT, // "content_source_url": CONTENT_SOURCE_URL // }, // //若新增的是多图文素材,则此处应还有几段articles结构 // ] //} string isshow_cover_pic = ""; if (this.CheckFengMianShow.Checked) { isshow_cover_pic = "1"; } else { isshow_cover_pic = "0"; } string description = NoHTML(this.tbContent.InnerText.ToString()); string postData = "{\"articles\":[{\"title\":\"" + this.txttuwen_title.Value.ToString() + "\",\"thumb_media_id\":\"" + Session["imgmedia_id"].ToString() + "\",\"author\":\"" + this.txttuwen_author.Value.ToString() + "\",\"digest\":\"" + this.txtzhaiyao.InnerText.ToString() + "\",\"show_cover_pic\":\"" + isshow_cover_pic + "\",\"content\":\"" + description + "\",\"content_source_url\":\"" + this.txtYuanWenUrl.Text.ToString() + "\"}]}"; string posturl = string.Format("https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token={0}", Access_tokento); string jsonres = PostUrl(posturl, postData); if (jsonres.Contains("media_id")) { //使用前需要引用Newtonsoft.json.dll文件 JObject jsonObj = JObject.Parse(jsonres); if (this.txttoUserName.Value.ToString().Trim().Equals("请输入用户微信号")) { ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('请输入接收消息的用户微信号!');", true); return; } string posturls = "https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token=" + Access_tokento; //预览图文消息的json数据{ // "touser":"OPENID", 可改为对微信号预览,例如towxname:zhangsan // "mpnews":{ // "media_id":"123dsdajkasd231jhksad" // }, // "msgtype":"mpnews" //} string postDatas = "{\"towxname\":\"" + this.txttoUserName.Value.ToString() + "\",\"mpnews\":{\"media_id\":\"" + jsonObj["media_id"].ToString() + "\"},\"msgtype\":\"mpnews\"}"; string tuwenres = wxs.GetPage(posturls, postDatas); //使用前需药引用Newtonsoft.json.dll文件 JObject jsonObjss = JObject.Parse(tuwenres); if (jsonObjss["errcode"].ToString().Equals("0")) { ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('发送预览成功!!');", true); return; } else { ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('发送预览失败!!');", true); return; } } } public static string NoHTML(string Htmlstring) { //删除脚本 Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase); //替换标签 Htmlstring = Htmlstring.Replace("\r\n", " "); Htmlstring = Htmlstring.Replace("\"", "'"); Htmlstring = Htmlstring.Replace("&nbsp;", " "); return Htmlstring; }

单击确定按钮代码如下:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjwjxw.html