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

/// <summary> /// 确认选择 /// </summary> /// <param></param> /// <param></param> protected void LinkBtnSubSave_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); return; } if (this.ImgTuWen2.ImageUrl.ToString().Equals("")) { ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('必须上传一张图片!');", true); return; } if (String.IsNullOrWhiteSpace(this.tbContent.InnerText.ToString())) { ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('请输入正文内容!');", true); 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); WxMpNewsInfo wmninfo = new WxMpNewsInfo(); wmninfo.title = this.txttuwen_title.Value.ToString(); wmninfo.contents = description.ToString(); wmninfo.ImageUrl = this.ImgTuWen.ImageUrl.ToString(); Session["wmninfo"] = wmninfo; Response.Redirect("WxMassManage.aspx?media_id=" + jsonObj["media_id"].ToString()); } } /// <summary> /// 请求Url,发送数据 /// </summary> public static string PostUrl(string url, string postData) { byte[] data = Encoding.UTF8.GetBytes(postData); // 设置参数 HttpWebRequest request = WebRequest.Create(url) 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; Stream outstream = request.GetRequestStream(); outstream.Write(data, 0, data.Length); outstream.Close(); //发送请求并获取相应回应数据 HttpWebResponse response = request.GetResponse() as HttpWebResponse; //直到request.GetResponse()程序才开始向目标网页发送Post请求 Stream instream = response.GetResponseStream(); StreamReader sr = new StreamReader(instream, Encoding.UTF8); //返回结果网页(html)代码 string content = sr.ReadToEnd(); return content; }

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

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