C#微信公众平台开发

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

C#微信公众平台开发涉及access_token的获取请参考《C#微信公众平台开发—access_token的获取存储与更新》一、为了实现高级群发功能,需要解决的问题1、通过微信接口上传图文消息素材时,Json中的图片不是url而是media_id,如何通过微信接口上传图片并获取图片的media_id?2、图片存储在什么地方,如何获取?二、实现步骤,以根据OpenID列表群发图文消息为例1、准备数据我把数据存储在数据库中,ImgUrl字段是图片在服务器上的相对路径(这里的服务器是自己的服务器,不是微信的哦)。从数据库中获取数据放到DataTable中:DataTabledt=ImgItemDal.GetImgItemTable(loginUser.OrgID,data);2、通过微信接口上传图片,返回图片的media_id取ImgUrl字段数据,通过MapPath方法获取图片在服务器上的物理地址,用FileStream类读取图片,并上传给微信HTTP上传文件代码(HttpRequestUtil类):///<summary>///Http上传文件///</summary>publicstaticstringHttpUploadFile(stringurl,stringpath){//设置参数HttpWebRequestrequest=WebRequest.Create(url)asHttpWebRequest;CookieContainercookieContainer=newCookieContainer();request.CookieContainer=cookieContainer;request.AllowAutoRedirect=true;request.Method=POST;stringboundary=DateTime.Now.Ticks.ToString(X);//随机分隔线request.ContentType=multipart/form-data;charset=utf-8;boundary=+boundary;byte[]itemBoundaryBytes=Encoding.UTF8.GetBytes(\r\n--+boundary+\r\n);byte[]endBoundaryBytes=Encoding.UTF8.GetBytes(\r\n--+boundary+--\r\n);intpos=path.LastIndexOf(\\);stringfileName=path.Substring(pos+1);//请求头部信息StringBuildersbHeader=newStringBuilder(string.Format(Content-Disposition:form-data;name=\file\;filename=\{0}\\r\nContent-Type:application/octet-stream\r\n\r\n,fileName));byte[]postHeaderBytes=Encoding.UTF8.GetBytes(sbHeader.ToString());FileStreamfs=newFileStream(path,FileMode.Open,FileAccess.Read);byte[]bArr=newbyte[fs.Length];fs.Read(bArr,0,bArr.Length);fs.Close();StreampostStream=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();//发送请求并获取相应回应数据HttpWebResponseresponse=request.GetResponse()asHttpWebResponse;//直到request.GetResponse()程序才开始向目标网页发送Post请求Streaminstream=response.GetResponseStream();StreamReadersr=newStreamReader(instream,Encoding.UTF8);//返回结果网页(html)代码stringcontent=sr.ReadToEnd();returncontent;}请求微信接口,上传图片,返回media_id(WXApi类):///<summary>///上传媒体返回媒体ID///</summary>publicstaticstringUploadMedia(stringaccess_token,stringtype,stringpath){//设置参数stringurl=string.Format(={0}&type={1},access_token,type);returnHttpRequestUtil.HttpUploadFile(url,path);}stringmsg=WXApi.UploadMedia(access_token,image,path);//上图片返回媒体IDstringmedia_id=Tools.GetJsonValue(msg,media_id);传入的path(aspx.cs文件中的代码):stringpath=MapPath(data);一个图文消息由若干条图文组成,每条图文有标题、内容、链接、图片等遍历每条图文数据,分别请求微信接口,上传图片,获取media_id3、上传图文消息素材拼接图文消息素材Json字符串(ImgItemDal类(操作图文消息表的类)):///<summary>///拼接图文消息素材Json字符串///</summary>publicstaticstringGetArticlesJsonStr(PageBasepage,stringaccess_token,DataTabledt){StringBuildersbArticlesJson=newStringBuilder();sbArticlesJson.Append({\articles\:[);inti=0;foreach(DataRowdrindt.Rows){stringpath=page.MapPath(dr[ImgUrl].ToString());if(!File.Exists(path)){return{\code\:0,\msg\:\要发送的图片不存在\};}stringmsg=WXApi.UploadMedia(access_token,image,path);//上图片返回媒体IDstringmedia_id=Tools.GetJsonValue(msg,media_id);sbArticlesJson.Append({);sbArticlesJson.Append(\thumb_media_id\:\+media_id+\,);sbArticlesJson.Append(\author\:\+dr[Author].ToString()+\,);sbArticlesJson.Append(\title\:\+dr[Title].ToString()+\,);sbArticlesJson.Append(\content_source_url\:\+dr[TextUrl].ToString()+\,);sbArticlesJson.Append(\content\:\+dr[Content].ToString()+\,);sbArticlesJson.Append(\digest\:\+dr[Content].ToString()+\,);if(i==dt.Rows.Count-1){sbArticlesJson.Append(\show_cover_pic\:\1\});}else{sbArticlesJson.Append(\show_cover_pic\:\1\},);}i++;}sbArticlesJson.Append(]});returnsbArticlesJson.ToString();}上传图文消息素材,获取图文消息的media_id:///<summary>///请求Url,发送数据///</summary>publicstaticstringPostUrl(stringurl,stringpostData){byte[]data=Encoding.UTF8.GetBytes(postData);//设置参数HttpWebRequestrequest=WebRequest.Create(url)asHttpWebRequest;CookieContainercookieContainer=newCookieContainer();request.CookieContainer=cookieContainer;request.AllowAutoRedirect=true;request.Method=POST;request.ContentType=application/x-=data.Length;Streamoutstream=request.GetRequestStream();outstream.Write(data,0,data.Length);outstream.Close();//发送请求并获取相应回应数据HttpWebResponseresponse=request.GetResponse()asHttpWebResponse;//直到request.GetResponse()程序才开始向目标网页发送Post请求Streaminstream=response.GetResponseStream();StreamReadersr=newStreamReader(instream,Encoding.UTF8);//返回结果网页(html)代码stringcontent=sr.ReadToEnd();returncontent;}///<summary>///上传图文消息素材返回media_id///</summary>publicstaticstringUploadNews(stringaccess_token,stringpostData){returnHttpRequestUtil.PostUrl(string.Format(={0},access_token),postData);}stringarticlesJson=ImgItemDal.GetArticlesJsonStr(this,access_token,dt);stringnewsMsg=WXApi.UploadNews(access_token,articlesJson);stringnewsid=Tools.GetJsonValue(newsMsg,media_id)4、群发图文消息获取全部关注者OpenID集合(WX

1 / 18
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功