• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ASP.NETCore上传微信永久视频素材

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

话不多说直接上源码

请求实体
 public class AddVideoRequest
    {
        /// <summary>
        /// 文件流
        /// </summary>
        public System.IO.Stream stream { get; set; }
        /// <summary>
        /// 文件名称
        /// </summary>
        public string name { get; set; }
        /// <summary>
        /// 文件全称,带后缀
        /// </summary>
        public string file_name { get; set; }
    
        /// <summary>
        /// 字节长度
        /// </summary>
        public long length { get; set; }
        /// <summary>
        /// 视频标题
        /// </summary>
        public string title { get; set; }
        /// <summary>
        /// 视频描述
        /// </summary>
        public string describe { get; set; }
    }


上传方法
public async Task<ResponseModel<AddMediaResponse>> AddVideo(AddVideoRequest video_request, string access_token) { try { string url = $"https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={access_token}&type=video"; 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", video_request.file_name)); byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString()); Stream fs = video_request.stream; byte[] bArr = new byte[fs.Length]; fs.Read(bArr, 0, bArr.Length); fs.Close(); Stream postStream = await request.GetRequestStreamAsync(); postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length); postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length); postStream.Write(bArr, 0, bArr.Length); postStream.Write(Encoding.UTF8.GetBytes("--" + boundary + "\r\n")); postStream.Write(Encoding.UTF8.GetBytes("Content-Disposition: form-data; name=\"description\";\r\n\r\n")); postStream.Write(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new { title = video_request.title, introduction = video_request.describe }))); postStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length); HttpWebResponse response = await request.GetResponseAsync() as HttpWebResponse; Stream instream = response.GetResponseStream(); StreamReader sr = new StreamReader(instream, Encoding.UTF8); string content = sr.ReadToEnd(); var res = JsonConvert.DeserializeObject<AddMediaResponse>(content); return new ResponseModel<AddMediaResponse> { success = res.errcode == 0, data = res, error_message = res.errmsg, body = content }; } catch (Exception ex) { return new ResponseModel<AddMediaResponse> { success = false, error_message = ex.Message }; } }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
ASP.NET页面间传值发布时间:2022-07-10
下一篇:
asp.net获取当前页面文件名,参数,域名等方法发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap