调微信接口之上传图文消息内的图片获取URL

新增永久图文素材中的content参数,官方给出的解释为:图文消息的具体内容,支持HTML标签,必须少于2万字符,小于1M,且此处会去除JS,涉及图片url必须来源 "上传图文消息内的图片获取URL"接口获取。外部图片url将被过滤。也就是说如果我们在content中传入的内容包含了src是别的网站的image的话,该图片是无法添加和显示的,我们需要先用上传图文消息内的图片获取URL该接口将图片上传到微信公众号服务器上,才能用

那么就开始调上传图文消息内的图片获取URL这个接口吧!

微信接口文档上的解释让我一直很迷惑:

调微信接口之上传图文消息内的图片获取URL_第1张图片

我不知道这个media参数应当怎么传!捣鼓了好半天,也报过

{"errcode":41001,"errmsg":"access_token missing hint: [e7hrga0305vr30!]"}这样的错,改来改去反正最后是对了!

以下是代码

说明:代码中需要修改appid和secret秘钥

        #region 上传图文素材的图片
        public static string  UploadImg(string imgPath)
        {
            //预见8订阅号的appid和secret
            string acc_tonke = CacheData.GetToken(DateTime.Now, "appid", "secret");//此处获取的公众号的appid和秘钥
            WebClient my = new WebClient();
            byte[] bArr = my.DownloadData(imgPath);

            string url = string.Format("https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token={0}", acc_tonke);
            string tockes= HttpUploadPhoto(url, imgPath, bArr);
            LogHelper.GetInstance().Info("上传图文素材的图片:" + tockes);

            return tockes;
        }
        #endregion

        public static string HttpUploadPhoto(string url, string path, byte[] bf)
        {
            //var accessToken = new WeiXin.IsExistAccess_Token();
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            CookieContainer cookieContainer = new CookieContainer();
            request.CookieContainer = cookieContainer;
            request.AllowAutoRedirect = true;
            request.Method = "POST";
            string boundary = 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");
            int pos = path.LastIndexOf("\\");
            string fileName = path.Substring(pos + 1);
            //请求头部信息 
            StringBuilder sbHeader = new StringBuilder(string.Format("Content-Disposition:form-data;name=\"media\";filename=\"{0}\"\r\nContent-Type:application/octet-stream\r\n\r\n", fileName));
            byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString());
            Stream postStream = request.GetRequestStream();
            postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
            postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
            postStream.Write(bf, 0, bf.Length);
            postStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
            postStream.Close();
            //发送请求并获取相应回应数据
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            Stream instream = response.GetResponseStream();
            StreamReader sr = new StreamReader(instream, Encoding.UTF8);
            string content = sr.ReadToEnd();
            return content;
        }

调用代码:

 //上传图文素材的图片
 string imgPath = "http://n.sinaimg.cn/news/crawl/84/w550h334/20180817/1c0m-hhvciiw3499049.jpg";
 string cc =UploadImg(imgPath);

得到的日志:

日志内容:上传图文素材的图片:

{"url":"http:\/\/mmbiz.qpic.cn\/mmbiz_jpg\/4lBqd7JQyt90mibibEqksYChMMSrELnYsSrhBMTxrBL6mKWZsOqAA5xHibuEN7jX549PW2juF1ORMwLWrbJTJHK6w\/0"}

成功了!

评论中有兄弟提到代码不能直接用,已整理源码上传:源码下载

 

你可能感兴趣的:(.net)