日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#微信公众号开发之上传永久素材

發布時間:2024/1/18 C# 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#微信公众号开发之上传永久素材 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Http幫助類

using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Web; ? namespace WpfWx {public class HttpHelper{public static string Get(string url){var webReq = (HttpWebRequest)WebRequest.Create(new Uri(url)); ?webReq.KeepAlive = false;webReq.Method = "GET";webReq.Timeout = 20000;webReq.ProtocolVersion = HttpVersion.Version11;webReq.ContentType = "application/x-www-form-urlencoded"; ?var response = (HttpWebResponse)webReq.GetResponse();var readStream = new StreamReader(response.GetResponseStream(), Encoding.UTF8);Char[] read = new Char[256];var count = readStream.Read(read, 0, 256);var result = string.Empty;while (count > 0){result += new String(read, 0, count);count = readStream.Read(read, 0, 256);}response.Close();readStream.Close();return result;} ? ?/// <summary>/// POST請求/// </summary>/// <param name="url"></param>/// <param name="postData"></param>/// <param name="headers"></param>/// <returns></returns>public static string Post(string url, string postData, Dictionary<string, string> headers = null, string contentType = null){var webReq = (HttpWebRequest)WebRequest.Create(new Uri(url)); ?Encoding encode = System.Text.Encoding.GetEncoding("utf-8");byte[] bytes = encode.GetBytes(postData); ?webReq.KeepAlive = false;webReq.Method = "POST";webReq.Timeout = 20000;webReq.ProtocolVersion = HttpVersion.Version11;if (contentType == null)webReq.ContentType = "application/x-www-form-urlencoded";elsewebReq.ContentType = contentType; ?webReq.ContentLength = bytes.Length;webReq.UserAgent = "Mozilla/5.0";if (headers != null){foreach (var header in headers)webReq.Headers.Add(header.Key, header.Value);} ?Stream outStream = webReq.GetRequestStream();outStream.Write(bytes, 0, bytes.Length);outStream.Close(); ?var response = (HttpWebResponse)webReq.GetResponse();var readStream = new StreamReader(response.GetResponseStream(), Encoding.UTF8);Char[] read = new Char[256];var count = readStream.Read(read, 0, 256);var result = string.Empty;while (count > 0){result += new String(read, 0, count);count = readStream.Read(read, 0, 256);}response.Close();readStream.Close();return result;}} } ?

1、最近更新:永久圖片素材新增后,將帶有URL返回給開發者,開發者可以在騰訊系域名內使用(騰訊系域名外使用,圖片將被屏蔽)。

2、公眾號的素材庫保存總數量有上限:圖文消息素材、圖片素材上限為100000,其他類型為1000。

3、素材的格式大小等要求與公眾平臺官網一致:

圖片(image): 2M,支持bmp/png/jpeg/jpg/gif格式

語音(voice):2M,播放長度不超過60s,mp3/wma/wav/amr格式

視頻(video):10MB,支持MP4格式

縮略圖(thumb):64KB,支持JPG格式

4、圖文消息的具體內容中,微信后臺將過濾外部的圖片鏈接,圖片url需通過"上傳圖文消息內的圖片獲取URL"接口上傳圖片獲取。

5、"上傳圖文消息內的圖片獲取URL"接口所上傳的圖片,不占用公眾號的素材庫中圖片數量的100000個的限制,圖片僅支持jpg/png格式,大小必須在1MB以下。

6、圖文消息支持正文中插入自己帳號和其他公眾號已群發文章鏈接的能力。

新增永久圖文素材

接口調用請求說明

http請求方式: POST,https協議 https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=ACCESS_TOKEN

調用示例

{"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,"need_open_comment":1,"only_fans_can_comment":1 },//若新增的是多圖文素材,則此處應還有幾段articles結構 ] }

參數說明

參數是否必須說明
title標題
thumb_media_id圖文消息的封面圖片素材id(必須是永久mediaID)
author作者
digest圖文消息的摘要,僅有單圖文消息才有摘要,多圖文此處為空。如果本字段為沒有填寫,則默認抓取正文前64個字。
show_cover_pic是否顯示封面,0為false,即不顯示,1為true,即顯示
content圖文消息的具體內容,支持HTML標簽,必須少于2萬字符,小于1M,且此處會去除JS,涉及圖片url必須來源 "上傳圖文消息內的圖片獲取URL"接口獲取。外部圖片url將被過濾。
content_source_url圖文消息的原文地址,即點擊“閱讀原文”后的URL
need_open_commentUint32 是否打開評論,0不打開,1打開
only_fans_can_commentUint32 是否粉絲才可評論,0所有人可評論,1粉絲才可評論

返回說明

{"media_id":MEDIA_ID }

返回的即為新增的圖文消息素材的media_id。

新增永久圖文消息

public string add_news(){var news = "{\"articles\":[{\"thumb_media_id\":\"GSEy95BsrOqFGxoyT-Vu9Ds6M-Rpt6A4XNoQ5BVQhrQ\",\"author\":\"PDF\",\"title\":\"圖文消息\",\"content_source_url\":\"www.baidu.com\",\"content\":\"http://mmbiz.qpic.cn/sz_mmbiz_jpg/7aDUwWPgLtRrPX6G7Jsg1I1QP7OuwaSRUGXnO95w5wITjt6FbaDyvMickQXejI5629iaaaH3YPRlbHke8nKicZpQg/0 \",\"digest\":\"圖文消息測試摘要\",\"show_cover_pic\":1}]}";var newsUrl = "https://api.weixin.qq.com/cgi-bin/material/add_news?access_token={0}";newsUrl = string.Format(newsUrl, accessToken);var result = HttpHelper.Post(newsUrl, news, null, "applicaion/json");return result;}

上傳圖文消息內的圖片獲取URL

本接口所上傳的圖片不占用公眾號的素材庫中圖片數量的100000個的限制。圖片僅支持jpg/png格式,大小必須在1MB以下。

接口調用請求說明

http請求方式: POST,https協議 https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=ACCESS_TOKEN 調用示例(使用curl命令,用FORM表單方式上傳一個圖片): curl -F media=@test.jpg "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=ACCESS_TOKEN"

參數說明

參數是否必須說明
access_token調用接口憑證
mediaform-data中媒體文件標識,有filename、filelength、content-type等信息

返回說明?正常情況下的返回結果為:

{"url": "http://mmbiz.qpic.cn/mmbiz/gLO17UPS6FS2xsypf378iaNhWacZ1G1UplZYWEYfwvuU6Ont96b1roYs CNFwaRrSaKTPCUdBK9DgEHicsKwWCBRQ/0" }

其中url就是上傳圖片的URL,可放置圖文消息中使用。

public string add_pic(string file) {string fileName = file;string url = string.Format("https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token={0}", accessToken);FileStream fs = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Read);byte[] fileByte = new byte[fs.Length];fs.Read(fileByte, 0, fileByte.Length);fs.Close();// 設置參數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"); ?//請求頭部信息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(fileByte, 0, fileByte.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;}

新增其他類型永久素材

接口調用請求說明

通過POST表單來調用接口,表單id為media,包含需要上傳的素材內容,有filename、filelength、content-type等信息。請注意:圖片素材將進入公眾平臺官網素材管理模塊中的默認分組。

http請求方式: POST,需使用https https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=ACCESS_TOKEN&type=TYPE 調用示例(使用curl命令,用FORM表單方式新增一個其他類型的永久素材,curl命令的使用請自行查閱資料)

參數說明

參數是否必須說明
access_token調用接口憑證
type媒體文件類型,分別有圖片(image)、語音(voice)、視頻(video)和縮略圖(thumb)
mediaform-data中媒體文件標識,有filename、filelength、content-type等信息

新增永久視頻素材需特別注意

在上傳視頻素材時需要POST另一個表單,id為description,包含素材的描述信息,內容格式為JSON,格式如下:

{"title":VIDEO_TITLE,"introduction":INTRODUCTION }

新增永久視頻素材的調用示例:

curl "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=ACCESS_TOKEN&type=TYPE" -F media=@media.file -F description='{"title":VIDEO_TITLE, "introduction":INTRODUCTION}'

參數說明

參數是否必須說明
title視頻素材的標題
introduction視頻素材的描述

返回說明

{"media_id":MEDIA_ID,"url":URL }

返回參數說明

參數描述
media_id新增的永久素材的media_id
url新增的圖片素材的圖片URL(僅新增圖片素材時會返回該字段)

錯誤情況下的返回JSON數據包示例如下(示例為無效媒體類型錯誤):

{"errcode":40007,"errmsg":"invalid media_id"}

新增圖片、視頻、聲音、縮略圖

public string add_material(string file){//(image): 2M,支持bmp/png/jpeg/jpg/gif格式//(voice):2M,播放長度不超過60s,mp3/wma/wav/amr格式//(video):10MB,支持MP4格式//(thumb):64KB,支持JPG格式 ? ?string fileName = file;string url = string.Format("https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={0}&type={1}", accessToken, "image");FileStream fs = new FileStream(file, FileMode.OpenOrCreate,FileAccess.Read);byte[] fileByte = new byte[fs.Length];fs.Read(fileByte, 0, fileByte.Length);fs.Close();// 設置參數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");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(fileByte, 0, fileByte.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;}

?

?

總結

以上是生活随笔為你收集整理的C#微信公众号开发之上传永久素材的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。