微信开发,群发接口和素材管理里,关于php 上传永久图文素材图文的区别

只需一步,快速开始
后使用快捷导航没有帐号?
所属分类: &
本版块为微信公众号开发教程与公众号开发相关技术分享板块,技术提问请到其他对应的问答板块发帖:)
微信公众号开发之群发图文接口C#
查看: 2268|回复: 3
& 主题帖子积分
本篇将介绍如何群发图文信息,上传图文信息所需的素材,界面如下:
09931.png (67.28 KB, 下载次数: 1)
17:23 上传
我们先看从素材库中获取图文素材的代码,界面:
25367.png (75.33 KB, 下载次数: 0)
17:23 上传
素材列表,我是使用的repeater控件,
前台代码如下:
&!--弹出选择素材窗口--&
&&&div id=&shownewgroup&&
&&&div class=&closeLogin& style=&height:40 background-color:#ddd9 line-height:40&&&span style=&float: color:#000; font-size:14 text-indent:5&&选择素材&/span&
& &&span style=&float:margin-left:20&&&a href=&WxNewTuWen.aspx& style=&color:& onclick=&hrefurl();& class=&hrefurl&&新建图文素材&/a&&/span&
& &&a class=&closeloginpage&&&img src=&images/close1.png& alt=&& /&&/a&&&&/div&
& &
&&&div style=&height:455 width:100%;&&
& &&asp:UpdatePanel ID=&UpdatePanel2& runat=&server&&
& & &ContentTemplate&
& && &&div style=&width:100%; height:35 margin:10&&&asp:LinkButton ID=&LinkBtnSelect& runat=&server& OnClick=&LinkBtnSelect_Click& &&div style=&background-image:url('images/buttonbg.png'); width:111 height:35 float: line-height:35 font-weight: text-align:color:#&&确认选择&/div&&/asp:LinkButton&
& && & &span style=&float:margin-left:20&&&asp:LinkButton ID=&LinkbtnRefresh& CssClass=&LinkbtnRefresh& runat=&server& OnClick=&LinkbtnRefresh_Click&&&div style=&background-image:url('images/buttonbg.png'); width:111 height:35 line-height:35 font-weight: text-align:color:#&&刷新&/div&&/asp:LinkButton&&/span&
& && &&span style=&float:margin-left:20&&&asp:LinkButton ID=&LinkBtnDelete& CssClass=&LinkbtnRefresh& runat=&server& OnClick=&LinkBtnDelete_Click&&&div style=&background-image:url('images/buttonbg.png'); width:111 height:35 line-height:35 font-weight: text-align:color:#&&删除素材&/div&&/asp:LinkButton&&/span&
& && &&/div&
& &&div style=&word-wrap:break-& id=&lbnewssucai& runat=&server&&
& & &asp:Repeater ID=&Repeatersucailist& runat=&server& OnItemDataBound=&Repeatersucailist_ItemDataBound&&
& &&&&ItemTemplate&
& && &&table style=&width:100%; border-top:1px solid #edc9 border-collapse: font-size:12& &
& && & &tr&
& && &&&&td style=&width:100&&&asp:Image ID=&ImageUrl& CssClass=&fenmianstyle2& runat=&server& /&&/td&
& && &&&&td style=&text-align: width:470 &&
& && && &&asp:Repeater ID=&Repeatersucailist2& runat=&server&&
& && && & &ItemTemplate&
& && && &&&&ul style=&margin:0padding:0&&
& && && && &&li&&%# Eval(&title&) %&&/li&
& && && &&&&/ul&
& && && & &/ItemTemplate&
& && && &&/asp:Repeater&
& && &&&&/td&
& && &&&&td style=&width:130&&
& && && &&asp:Label ID=&lbUpate_time& runat=&server& Text=&Label&&&/asp:Label&
& && &&&&/td&
& && &&&&td style=&width:50 text-align:&&
& && && &&asp:CheckBox ID=&CheckIn& runat=&server& /&
& && && &&asp:Label ID=&lbmedia_id& runat=&server& Visible=&false& Text=&&&&/asp:Label&
& && &&&&/td&
& && & &/tr&
& && &&/table&
& &&&&/ItemTemplate&
& & &/asp:Repeater&
& & &div style=&font-size:14 height:30 line-height:30 text-indent:10 border-top:1px solid #ced9&&
& &&&&span style=&float:&&本类型素材总数量为:&/span&&span style=&float: color:&&&asp:Label ID=&lbtotal_count& runat=&server& Text=&0&&&/asp:Label&&/span&&&
& &&&&span style=&float: margin-left:20&&本次获取的素材数量为:&/span&&span style=&float: color:&&&asp:Label ID=&lbitem_count& runat=&server& Text=&0&&&/asp:Label&&/span&
& & &/div&
& &&/div&
& & &/ContentTemplate&
& &&/asp:UpdatePanel&
&&&/div&
&div id=&shownewgroupzhezhaoceng&&&/div&复制代码
后台代码如下:
/// &summary&
/// 绑定图文素材列表
/// &/summary&
private void BindNewsSucaiList()
{
&&WeiXinServer wxs = new WeiXinServer();
&&string res = &&;
&&///从缓存读取accesstoken
&&string Access_token = Cache[&Access_token&]
&&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);
&&string posturl = &https://api./cgi-bin/material/batchget_material?access_token=& + Access_
&&//POST数据例子: POST数据例子:{&type&:TYPE,&offset&:OFFSET,&count&:COUNT}
&&string postData = &{\&type\&:\&news\&,\&offset\&:\&0\&,\&count\&:\&20\&}&;
&&res = wxs.GetPage(posturl, postData);
&&//使用前需要引用Newtonsoft.json.dll文件
&&JObject jsonObj = JObject.Parse(res);
&&int groupsnum = jsonObj[&item&].Count();
&&List&WxNewsSucaiIteminfo& newssucaiitemlist = new List&WxNewsSucaiIteminfo&();
&&List&WxNewsSuCaiItemlistinfo& WxNewsSuCaiItemlist = new List&WxNewsSuCaiItemlistinfo&();
&&for (int i = 0; i & i++)
&&{
& &WxNewsSucaiIteminfo newssucaiitem = new WxNewsSucaiIteminfo();
& &newssucaiitem.media_id = jsonObj[&item&][i][&media_id&].ToString();
& &newssucaiitem.update_time = jsonObj[&item&][i][&update_time&].ToString();
& &newssucaiitem.total_count = jsonObj[&total_count&].ToString();
& &newssucaiitem.item_count = jsonObj[&item_count&].ToString();
& &newssucaiitemlist.Add(newssucaiitem);
& &int news_itemcount = jsonObj[&item&][i][&content&][&news_item&].Count();
& &if (news_itemcount & 0)
& &{
& & for (int j = 0; j & news_ j++)
& & {
& &&&WxNewsSuCaiItemlistinfo wnscilinfo = new WxNewsSuCaiItemlistinfo();
& &&&wnscilinfo.title = jsonObj[&item&][i][&content&][&news_item&][j][&title&].ToString();
& &&&wnscilinfo.thumb_media_id = jsonObj[&item&][i][&content&][&news_item&][j][&thumb_media_id&].ToString();
& &&&wnscilinfo.show_cover_pic = int.Parse(jsonObj[&item&][i][&content&][&news_item&][j][&show_cover_pic&].ToString());
& &&&wnscilinfo.author = jsonObj[&item&][i][&content&][&news_item&][j][&author&].ToString();
& &&&wnscilinfo.digest = jsonObj[&item&][i][&content&][&news_item&][j][&digest&].ToString();
& &&&wnscilinfo.content = jsonObj[&item&][i][&content&][&news_item&][j][&content&].ToString();
& &&&wnscilinfo.url = jsonObj[&item&][i][&content&][&news_item&][j][&url&].ToString();
& &&&wnscilinfo.content_source_url = jsonObj[&item&][i][&content&][&news_item&][j][&content_source_url&].ToString();
& &&&wnscilinfo.media_id = newssucaiitem.media_id.ToString();
& &&&WxNewsSuCaiItemlist.Add(wnscilinfo);
& & }
& &}
&&}
&&Session[&WxNewsSuCaiItemlist&] = WxNewsSuCaiI
&&this.Repeatersucailist.DataSource =
&&this.Repeatersucailist.DataBind();
07086.png (215.57 KB, 下载次数: 1)
17:24 上传
29915.png (104.8 KB, 下载次数: 0)
17:25 上传
新建单图文上传封面,删除封面的代码如下:
/// &summary&
/// &/summary&上传图片文件
/// &param name=&sender&&&/param&
/// &param name=&e&&&/param&
protected void LinkBtnFileUploadImg_Click(object sender, EventArgs e)
{
&&if (this.FileUploadImg.HasFile)
&&{
& &string fileContentType = FileUploadImg.PostedFile.ContentT
& &if (fileContentType == &image/bmp& || fileContentType == &image/gif& || fileContentType == &image/png& || fileContentType == &image/x-png& || fileContentType == &image/jpeg&
& &&&|| fileContentType == &image/pjpeg&)
& &{
& & int fileSize = this.FileUploadImg.PostedFile.ContentL
& & if (fileSize &=2097152)
& & {
& &&&string fileName = this.FileUploadImg.PostedFile.FileN
& &&&// 客户端文件路径
& &&&string filepath = FileUploadImg.PostedFile.FileN //得到的是文件的完整路径,包括文件名,如:C:\Documents and Settings\Administrator\My Documents\My Pictures\_m.jpg
& &&&//string filepath = FileUpload1.FileN& & //得到上传的文件名_m.jpg
& &&&string filename = filepath.Substring(filepath.LastIndexOf(&\\&) + 1);//_m.jpg
& &&&string serverpath = Server.MapPath(&~/WeiXinImg/&) +//取得文件在服务器上保存的位置C:\Inetpub\wwwroot\WebSite1\images\_m.jpg
& &&&this.ImgTuWen.ImageUrl = &~/WeiXinImg/& + FileUploadImg.FileN
& &&&this.ImgTuWen2.Visible =
& &&&this.ImgTuWen2.ImageUrl = &~/WeiXinImg/& + FileUploadImg.FileN
& &&&this.FileUploadImg.PostedFile.SaveAs(serverpath);//将上传的文件另存为
& &&&this.LinkBtnDeleteImg.Visible =
& &&&Session[&fileNameimg&] = this.FileUploadImg.PostedFile.FileN
& &&&//上传临时图片素材至微信服务器,3天后微信服务器会自动删除
& &&&WeiXinServer wxs = new WeiXinServer();
& &&&///从缓存读取accesstoken
& &&&string Access_token = Cache[&Access_token&]
& &&&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);
& &&&//WebClient wx_upload = new WebClient();
& &&&//wx_upload.Credentials = CredentialCache.DefaultC
& &&&string url = string.Format(&http://file.api./cgi-bin/media/upload?access_token={0}&type={1}&, Access_tokento, &image&);
& &&&string result = HttpUploadFile(url, serverpath);
& &&&if (result.Contains(&media_id&))
& &&&{
& && &//使用前需要引用Newtonsoft.json.dll文件
& && &JObject jsonObj = JObject.Parse(result);
& && &Session[&imgmedia_id&] = jsonObj[&media_id&].ToString();
& &&&}
& &&&Response.Write(&&script&alert('上传图片成功!')&/script&&);
& & }
& & else
& & {
& &&&Response.Write(&&script&alert('上传文件不能大于2M!')&/script&&);
& & }
& &&&
& &}
& &else
& &{
& & Response.Write(&&script&alert('只支持BMP,GIF,PNG,JPG格式的图片!')&/script&&);
& &}
&&}
&&else
&&{
& &Response.Write(&&script&alert('请选择图片!')&/script&&);
&&}
/// &summary&
/// Http上传文件
/// &/summary&
public static string HttpUploadFile(string url, string path)
{
&&// 设置参数
&&HttpWebRequest request = WebRequest.Create(url) as HttpWebR
&&CookieContainer cookieContainer = new CookieContainer();
&&request.CookieContainer = cookieC
&&request.AllowAutoRedirect =
&&request.Method = &POST&;
&&string boundary = DateTime.Now.Ticks.ToString(&X&); // 随机分隔线
&&request.ContentType = &multipart/form-charset=utf-8;boundary=& +
&&byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes(&\r\n--& + boundary + &\r\n&);
&&byte[] endBoundaryBytes = Encoding.UTF8.GetBytes(&\r\n--& + boundary + &--\r\n&);
&&int pos = path.LastIndexOf(&\\&);
&&string fileName = path.Substring(pos + 1);
&&//请求头部信息
&&StringBuilder sbHeader = new StringBuilder(string.Format(&Content-Disposition:form-name=\&file\&;filename=\&{0}\&\r\nContent-Type:application/octet-stream\r\n\r\n&, fileName));
&&byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString());
&&FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
&&byte[] bArr = new byte[fs.Length];
&&fs.Read(bArr, 0, bArr.Length);
&&fs.Close();
&&Stream postStream = request.GetRequestStream();
&&postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
&&postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
&&postStream.Write(bArr, 0, bArr.Length);
&&postStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
&&postStream.Close();
&&//发送请求并获取相应回应数据
&&HttpWebResponse response = request.GetResponse() as HttpWebR
&&//直到request.GetResponse()程序才开始向目标网页发送Post请求
&&Stream instream = response.GetResponseStream();
&&StreamReader sr = new StreamReader(instream, Encoding.UTF8);
&&//返回结果网页(html)代码
&&string content = sr.ReadToEnd();
&&
/// &summary&
/// 删除图片
/// &/summary&
/// &param name=&sender&&&/param&
/// &param name=&e&&&/param&
protected void LinkBtnDeleteImg_Click(object sender, EventArgs e)
{
&&string filename = Session[&fileNameimg&].ToString();
&&if (!string.IsNullOrEmpty(filename))//确保picPath有值并且不为空。
&&{
& &
& &string serverpath = Server.MapPath(&~/WeiXinImg/&) +//取得文件在服务器上保存的位置C:\Inetpub\wwwroot\WebSite1\images\_m.jpg
& &if (File.Exists(serverpath))
& &{
& & try
& & {
& &&&File.Delete(serverpath);
& &&&this.ImgTuWen.ImageUrl = &weixinimg/fengmiandefault.jpg&;
& &&&this.ImgTuWen2.Visible =
& &&&this.ImgTuWen2.ImageUrl = &&;
& &&&Session[&fileNameimg&] =
& &&&this.LinkBtnDeleteImg.Visible =
& & }
& & catch(Exception ex)
& & {
& &&&//错误处理:
& &&&Response.Write(ex.Message.ToString());
& & }
& &}
&&}
新建单图文预览代码如下:
/// &summary&
/// 预览图文消息
/// &/summary&
/// &param name=&sender&&&/param&
/// &param name=&e&&&/param&
protected void LinkBtnSendPreview_Click(object sender, EventArgs e)
{
&&Session[&media_id&] =
&&//非空验证
&&if (String.IsNullOrWhiteSpace(this.txttuwen_title.Value.ToString()))
&&{
& &ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), &&, &alert('请输入图文标题!');&, true);
& &this.txttuwen_title.Focus();
& &
&&}
&&if (this.ImgTuWen2.ImageUrl.ToString().Equals(&&))
&&{
& &ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), &&, &alert('必须上传一张图片!');&, true);
& &this.ImgTuWen2.Focus();
& &
&&}
&&if (String.IsNullOrWhiteSpace(this.tbContent.InnerText.ToString()))
&&{
& &ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), &&, &alert('请输入正文内容!');&, true);
& &this.tbContent.Focus();
& &
&&}
&&//对各项进行赋值
&&WeiXinServer wxs = new WeiXinServer();
&&///从缓存读取accesstoken
&&string Access_token = Cache[&Access_token&]
&&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./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);
& &
& &}
& &string posturls = &https://api./cgi-bin/message/mass/preview?access_token=& + Access_
& &//预览图文消息的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);
& &
& &}
& &else
& &{
& & ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), &&, &alert('发送预览失败!!');&, true);
& &
& &}
&&}
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(& &, & &);
&&return H
单击确定按钮代码如下:
/// &summary&
/// 确认选择
/// &/summary&
/// &param name=&sender&&&/param&
/// &param name=&e&&&/param&
protected void LinkBtnSubSave_Click(object sender, EventArgs e)
{
&&Session[&media_id&] =
&&//非空验证
&&if (String.IsNullOrWhiteSpace(this.txttuwen_title.Value.ToString()))
&&{
& &ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), &&, &alert('请输入图文标题!');&, true);
& &
&&}
&&if (this.ImgTuWen2.ImageUrl.ToString().Equals(&&))
&&{
& &ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), &&, &alert('必须上传一张图片!');&, true);
& &
&&}
&&if (String.IsNullOrWhiteSpace(this.tbContent.InnerText.ToString()))
&&{
& &ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), &&, &alert('请输入正文内容!');&, true);
& &
&&}
&&//对各项进行赋值
&&WeiXinServer wxs = new WeiXinServer();
&&///从缓存读取accesstoken
&&string Access_token = Cache[&Access_token&]
&&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./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&] =
& &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 HttpWebR
&&CookieContainer cookieContainer = new CookieContainer();
&&request.CookieContainer = cookieC
&&request.AllowAutoRedirect =
&&request.Method = &POST&;
&&request.ContentType = &application/x-www-form-urlencoded&;
&&request.ContentLength = data.L
&&Stream outstream = request.GetRequestStream();
&&outstream.Write(data, 0, data.Length);
&&outstream.Close();
&&//发送请求并获取相应回应数据
&&HttpWebResponse response = request.GetResponse() as HttpWebR
&&//直到request.GetResponse()程序才开始向目标网页发送Post请求
&&Stream instream = response.GetResponseStream();
&&StreamReader sr = new StreamReader(instream, Encoding.UTF8);
&&//返回结果网页(html)代码
&&string content = sr.ReadToEnd();
&&
}复制代码Response.Redirect(&WxMassManage.aspx?media_id=& + jsonObj[&media_id&].ToString());复制代码
这句代码就是将上传图文后得到的media_Id参数传送到群发界面,群发界面接收代码如下:
protected void Page_Load(object sender, EventArgs e)
{
&&if(!Page.IsPostBack)
&&{
& &BindNewsSucaiList();//绑定素材列表
& &BindGroupList();//绑定分组列表
& &BindMassCount();//绑定本月已群发条数
& &this.DataBind();
& &if (Request.QueryString[&media_id&] != null)
& &{
& & this.RadioBtnList.SelectedValue = &1&;
& & this.showExpress.Visible =
& & this.txtwenben.Visible =
& & this.tuwen.Visible =
& & this.tuwenxuan.Visible =
& & this.tuwenjian.Visible =
& & this.lbtuwenmedai_id.Visible =
& & this.lbtuwenmedai_id.Text = Request.QueryString[&media_id&].ToString();
& & this.LinkBtndeletetuwen.Visible =
& & this.Imageyixuan.Visible =
& &}
&&}
最终界面如下:
24424.png (49.98 KB, 下载次数: 0)
17:27 上传
我这里只接收了一个media_id值,相对于做的简单,直接将值赋值给了一个label用于显示,也可以做成像官网那样,确定选择后,按照图文样式显示。
00498.png (97.85 KB, 下载次数: 1)
17:27 上传
最后一步:群发按钮代码:
/// &summary&
& &/// 群发
& &/// &/summary&
& &/// &param name=&sender&&&/param&
& &/// &param name=&e&&&/param&
& &protected void LinkBtnSubSend_Click(object sender, EventArgs e)
& &{
& &&&//根据单选按钮判断类型,//如果选择的是图文消息
& &&&if (this.RadioBtnList.SelectedValue.ToString().Equals(&1&))
& &&&{
& && & if (String.IsNullOrWhiteSpace(this.lbtuwenmedai_id.Text.ToString().Trim()))
& && & {
& && && &ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), &&, &alert('请选择或新建图文素材再进行群发!');&, true);
& && && &
& && & }
& && & WxMassService wms = new WxMassService();
& && & List&WxMassInfo& wxmaslist = wms.GetMonthMassCount();
& && & if (wxmaslist.Count &= 4)
& && & {
& && && &ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), &&, &alert('本月可群发消息数量已达上限!');&, true);
& && && &
& && & }
& && & else
& && & {
& && && &
& && && &//如何群发类型为全部用户,根据openID列表群发给全部用户,订阅号不可用,服务号认证后可用
& && && &if (this.DDLMassType.SelectedValue.ToString().Equals(&0&))
& && && &{
& && && &&&StringBuilder sbs = new StringBuilder();
& && && &&&sbs.Append(GetAllUserOpenIDList());
& && && &&&WeiXinServer wxs = new WeiXinServer();
& && && &&&///从缓存读取accesstoken
& && && &&&string Access_token = Cache[&Access_token&]
& && && &&&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);
& && && &&&string posturl = &https://api./cgi-bin/message/mass/send?access_token=& + Access_
& && && &&&///群发POST数据示例如下:
& && && &&&// {
& && && &&&//&&&touser&:[
& && && &&&//&&&OPENID1&,
& && && &&&//&&&OPENID2&
& && && &&&//&&],
& && && &&&//&&&mpnews&:{
& && && &&&//& &&media_id&:&123dsdajkasd231jhksad&
& && && &&&//&&},
& && && &&&//&&&msgtype&:&mpnews&
& && && &&&//}
& && && &&&string postData = &{\&touser\&:[& + sbs.ToString() +
& && && && & &],\&mpnews\&:{\&media_id\&:\&& + this.lbtuwenmedai_id.Text.ToString() +
& && && && & &\&},\&msgtype\&:\&mpnews\&}&;
& && && &&&string tuwenres = wxs.GetPage(posturl, postData);
& && && &&&//使用前需药引用Newtonsoft.json.dll文件
& && && &&&JObject jsonObj = JObject.Parse(tuwenres);
& && && &&&if (jsonObj[&errcode&].ToString().Equals(&0&))
& && && &&&{
& && && && & Session[&media_id&] =
& && && && & WxMassInfo wmi = new WxMassInfo();
& && && && & if (Session[&wmninfo&] != null)
& && && && & {
& && && && && &WxMpNewsInfo wmninfo = Session[&wmninfo&] as WxMpNewsI
& && && && && &wmi.title = wmninfo.title.ToString();
& && && && && &wmi.contents = wmninfo.contents.ToString();
& && && && && &wmi.ImageUrl = wmninfo.ImageUrl.ToString();
& && && && && &wmi.type = &图文&;
& && && && && &if (this.DDLMassType.SelectedValue.ToString().Equals(&0&))
& && && && && &{
& && && && && &&&wmi.massObject = this.DDLMassType.SelectedItem.Text.ToString();
& && && && && &}
& && && && && &else
& && && && && &{
& && && && && &&&wmi.massObject = this.DDLGroupList.SelectedItem.Text.ToString();
& && && && && &}
& && && && && &wmi.massStatus = &成功&;//群发成功之后返回的状态码
& && && && && &wmi.massMessageID = jsonObj[&msg_id&].ToString();//群发成功之后返回的消息ID
& && && && && &wmi.massDate = System.DateTime.Now.ToString();
& && && && && &int num = wms.AddWxMassInfo(wmi);
& && && && && &if (num & 0)
& && && && && &{
& && && && && &&&Session[&wmninfo&] =
& && && && && &&&ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), &&, &alert('群发任务已提交成功!!!数据已保存!');location='WxMassManage.aspx';&, true);
& && && && && &&&
& && && && && &}
& && && && && &else
& && && && && &{
& && && && && &&&ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), &&, &alert('群发任务已提交成功!!!数据保存失败!');&, true);
& && && && && &&&
& && && && && &}
& && && && & }
& && && && & else
& && && && & {
& && && && && &wmi.title = &&;
& && && && && &wmi.contents = &&;
& && && && && &wmi.ImageUrl = &&;
& && && && && &wmi.type = &图文&;
& && && && && &if (this.DDLMassType.SelectedValue.ToString().Equals(&0&))
& && && && && &{
& && && && && &&&wmi.massObject = this.DDLMassType.SelectedItem.Text.ToString();
& && && && && &}
& && && && && &else
& && && && && &{
& && && && && &&&wmi.massObject = this.DDLGroupList.SelectedItem.Text.ToString();
& && && && && &}
& && && && && &wmi.massStatus = &成功&;//群发成功之后返回的状态码
& && && && && &wmi.massMessageID = jsonObj[&msg_id&].ToString();//群发成功之后返回的消息ID
& && && && && &wmi.massDate = System.DateTime.Now.ToString();
& && && && && &int num = wms.AddWxMassInfo(wmi);
& && && && && &if (num & 0)
& && && && && &{
& && && && && &&&Session[&wmninfo&] =
& && && && && &&&ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), &&, &alert('群发任务已提交成功!!!图文部分数据已保存!');location='WxMassManage.aspx';&, true);
& && && && && &&&
& && && && && &}
& && && && && &else
& && && && && &{
& && && && && &&&ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), &&, &alert('群发任务已提交成功!!!数据保存失败!');&, true);
& && && && && &&&
& && && && && &}
& && && && & }
& && && &&&}
& && && &&&else
& && && &&&{
& && && && & ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), &&, &alert('群发任务提交失败!!');&, true);
& && && && &
& && && &&&}
& && && &}
& && && &else
& && && &{
& && && &&&//根据分组进行群发,订阅号和服务号认证后均可用
& && && &&&string group_id = this.DDLGroupList.SelectedValue.ToString();
& && && &&&WeiXinServer wxs = new WeiXinServer();
& && && &&&///从缓存读取accesstoken
& && && &&&string Access_token = Cache[&Access_token&]
& && && &&&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);
& && && &&&string posturl = &https://api./cgi-bin/message/mass/send?access_token=& + Access_
& && && &&&///群发POST数据示例如下:
& && && &&&//&&{
& && && &&&//&&&filter&:{
& && && &&&//& &&is_to_all&:false
& && && &&&//& &&group_id&:&2&
& && && &&&//&&},
& && && &&&//&&&mpnews&:{
& && && &&&//& &&media_id&:&123dsdajkasd231jhksad&
& && && &&&//&&},
& && && &&&//&&&msgtype&:&mpnews&
& && && &&&//}
& && && &&&string postData = &{\&filter\&:{\&is_to_all\&:\&false\&\&group_id\&:\&&+group_id+
& && && && & &\&},\&mpnews\&:{\&media_id\&:\&& + this.lbtuwenmedai_id.Text.ToString() +
& && && && & &\&},\&msgtype\&:\&mpnews\&}&;
& && && &&&string tuwenres = wxs.GetPage(posturl, postData);
& && && &&&//使用前需药引用Newtonsoft.json.dll文件
& && && &&&JObject jsonObj = JObject.Parse(tuwenres);
& && && &&&if (jsonObj[&errcode&].ToString().Equals(&0&))
& && && &&&{
& && && && & Session[&media_id&] =
& && && && & WxMassInfo wmi = new WxMassInfo();
& && && && & if (Session[&wmninfo&] != null)
& && && && & {
& && && && && &WxMpNewsInfo wmninfo = Session[&wmninfo&] as WxMpNewsI
& && && && && &wmi.title = wmninfo.title.ToString();
& && && && && &wmi.contents = wmninfo.contents.ToString();
& && && && && &wmi.ImageUrl = wmninfo.ImageUrl.ToString();
& && && && && &wmi.type = &图文&;
& && && && && &if (this.DDLMassType.SelectedValue.ToString().Equals(&0&))
& && && && && &{
& && && && && &&&wmi.massObject = this.DDLMassType.SelectedItem.Text.ToString();
& && && && && &}
& && && && && &else
& && && && && &{
& && && && && &&&wmi.massObject = this.DDLGroupList.SelectedItem.Text.ToString();
& && && && && &}
& && && && && &wmi.massStatus = &成功&;//群发成功之后返回的状态码
& && && && && &wmi.massMessageID = jsonObj[&msg_id&].ToString();//群发成功之后返回的消息ID
& && && && && &wmi.massDate = System.DateTime.Now.ToString();
& && && && && &int num = wms.AddWxMassInfo(wmi);
& && && && && &if (num & 0)
& && && && && &{
& && && && && &&&Session[&wmninfo&] =
& && && && && &&&ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), &&, &alert('群发任务已提交成功!!!数据已保存!');location='WxMassManage.aspx';&, true);
& && && && && &&&
& && && && && &}
& && && && && &else
& && && && && &{
& && && && && &&&ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), &&, &alert('群发任务已提交成功!!!数据保存失败!');&, true);
& && && && && &&&
& && && && && &}
& && && && & }
& && && && & else
& && && && & {
& && && && && &wmi.title = &&;
& && && && && &wmi.contents = &&;
& && && && && &wmi.ImageUrl = &&;
& && && && && &wmi.type = &图文&;
& && && && && &if (this.DDLMassType.SelectedValue.ToString().Equals(&0&))
& && && && && &{
& && && && && &&&wmi.massObject = this.DDLMassType.SelectedItem.Text.ToString();
& && && && && &}
& && && && && &else
& && && && && &{
& && && && && &&&wmi.massObject = this.DDLGroupList.SelectedItem.Text.ToString();
& && && && && &}
& && && && && &wmi.massStatus = &成功&;//群发成功之后返回的状态码
& && && && && &wmi.massMessageID = jsonObj[&msg_id&].ToString();//群发成功之后返回的消息ID
& && && && && &wmi.massDate = System.DateTime.Now.ToString();
& && && && && &int num = wms.AddWxMassInfo(wmi);
& && && && && &if (num & 0)
& && && && && &{
& && && && && &&&Session[&wmninfo&] =
& && && && && &&&ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), &&, &alert('群发任务已提交成功!!!图文部分数据已保存!');location='WxMassManage.aspx';&, true);
& && && && && &&&
& && && && && &}
& && && && && &else
& && && && && &{
& && && && && &&&ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), &&, &alert('群发任务已提交成功!!!数据保存失败!');&, true);
& && && && && &&&
& && && && && &}
& && && && & }
& && && &&&}
& && && &&&else
& && && &&&{
& && && && & ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), &&, &alert('群发任务提交失败!!');&, true);
& && && && &
& && && &&&}
& && && &}
& && & }
& &&&}
& &}复制代码
为什么叫群发任务提交成功或失败,因为将信息提交给微信服务器,微信服务器还需审核,审核过程中也有可能审核不通过,不给于群发,所以我起名叫这个,嘿嘿,随便你们怎么起。。。。。
25927.jpg (255.03 KB, 下载次数: 0)
17:28 上传
至此群发图文信息功能,已完毕,最后是群发记录,还记得上一章提到的群发成功之后要在本地保存记录吗,保存记录的原因,用于计算当月已群发几条信息,另外还有一个功能就是,群发成功之后,会得到一个消息msgid,根据这个ID可以对已经发送成功的信息进行撤销(删除)操作,关于撤销操作:微信官方规定,对群发成功的图文和视频消息,半个小时之内可以进行删除操作,其他消息一经群发成功概不支持此操作。截图如下:
/// &summary&
/// 微信已群发消息实体类,用于记录已群发消息的条数,信息实体
/// &/summary&
public class WxMassInfo
{
& &public int WxMassNo { }//群发消息编号,数据库自增列
& &public string title { }//图文消息的标题,若消息是文本类型,此项不显示
& &public string ImageUrl { }//图片地址,若消息是文本类型,此项不显示
& &public string type { }//消息的类型,文本,图文,图片,语音,视频
& &public string contents { }//文本消息的内容,图文消息的正文
& &public string massObject { }//群发对象
& &public string massStatus { }//群发状态
& &public string massMessageID{ }//群发成功后返回的消息ID
& &public string massDate { }//群发日期时间
以上就是本文的全部内容,希望对大家的学习有所帮助。
& 主题帖子积分
新人求带, 积分 95, 距离下一级还需 55 积分
新人求带, 积分 95, 距离下一级还需 55 积分
个哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥
& 主题帖子积分
略知一二, 积分 150, 距离下一级还需 350 积分
略知一二, 积分 150, 距离下一级还需 350 积分
学习了.................................
& 主题帖子积分
略知一二, 积分 200, 距离下一级还需 300 积分
略知一二, 积分 200, 距离下一级还需 300 积分
这个不俗,这么多代码,学习
<是专业的第三方微信开发者平台,为生态而生。
本站为第三方微信开发者平台,非腾讯官方网站。
天津市滨海新区中新生态城中成大道生态建设公寓9号楼3层301
欢迎来这里一起喝喝茶,聊聊你的产品。
微信公众号gongzhongkaifa
工作日12小时内回复。
工作日12小时内回复。

我要回帖

更多关于 java上传图文消息素材 的文章

 

随机推荐