ASP.NETコアマイクロチャネルは、恒久的に映像素材をアップロードします

いくつかの単語の男は、直接ソースコードを、言いました

要求エンティティ
 パブリッククラスAddVideoRequest
    {
        /// <要約>
        ///文件流
        /// </要約>
        公共System.IO.Streamストリーム{取得します。セット; }
        /// <要約>
        ///ファイル名
        /// </要約>
        公共の文字列名{取得します。セット; }
        /// <要約>
        ///接尾辞完全ファイル名を
        /// </要約>
        パブリック文字列{file_nameのGET、SET;}
    
        /// <まとめ>
        ///バイト長
        /// </要約>
        公衆長い長さを取得または設定し;}
        /// <まとめ>
        ///ビデオタイトル
        /// </要約>
        公共GET {文字列のタイトル、SET;}
        /// <まとめ>
        ///説明ビデオ
        /// </要約>
        パブリック文字列{GETをDESCRIBE、SET;}
    }


上传方法
公共の 非同期タスク<ResponseModel <AddMediaResponse >> AddVideo(AddVideoRequestのvideo_request、文字列access_tokenは) { しようと { 文字列の URL = $ " {https://api.weixin.qq.com/cgi-bin/material/add_material?access_token= access_tokenは}&タイプ=動画" ; HttpWebRequestの要求 = WebRequest.Create(URL)などのHttpWebRequest。 CookieContainer cookieContainer = 新しいCookieContainer(); request.CookieContainer = cookieContainer。 request.AllowAutoRedirect =; request.Method = " POST " ; 文字列の境界= DateTime.Now.Ticks.ToString(" X "); // 随机分隔线 request.ContentType = " マルチパート/フォームデータ;のcharset = UTF-8;境界= " + 境界。 バイト [] itemBoundaryBytes = Encoding.UTF8.GetBytes(" \ R \ N-- " +境界+ " \ R \ n " ); バイト [] endBoundaryBytesの=のEncoding.UTF8.GetBytes(" " + +境界" - Rを\ n \ " )。 // 请求头部信息 のStringBuilder sbHeader = 新しい StringBuilderの(文字列 .Format(" コンテンツの廃棄:フォームデータ、名前= \"メディア\ ";ファイル名= \" {0} \ "\ R \ nContentタイプ:アプリケーション/ octet-streamの\ rを\ nの\ rをする\ n " 、video_request.file_name))。 バイト [] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString())。 ストリームFS = video_request.stream。 バイト []バール= 新しいです バイト[fs.Length]。 fs.Read(BARR、0 、bArr.Length)。 fs.Close(); ストリームpostStreamは = 待つ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(" - " +境界+ " \ R \ n " )); postStream.Write(Encoding.UTF8.GetBytes("コンテンツの廃棄:フォームデータ、名前= \"説明\ "; \ rをする\ n \ rをする\ n " )); postStream.Write(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(新しい {タイトル= video_request.title、導入= video_request.describe})))。 postStream.Write(endBoundaryBytes、0 、endBoundaryBytes.Length)。 HttpWebResponseの応答 =を待つ request.GetResponseAsyncを()としてHttpWebResponseの。 ストリームinStream = response.GetResponseStream()。 StreamReaderをSR =新しいStreamReaderを(インストリーム、Encoding.UTF8)。 文字列コンテンツ= sr.ReadToEnd()。 VaRの RES = JsonConvert.DeserializeObject <AddMediaResponse> コンテンツ)。 戻り 新しい ResponseModel <AddMediaResponse> { 成功 = res.errcodeの== 0 データ = RES、 ERROR_MESSAGE = res.errmsg、 本体 = コンテンツ }。 } キャッチ(例外例) { リターン 新しい ResponseModel <AddMediaResponse> {成功= 、ERROR_MESSAGE = ex.Message}。 } }

 

おすすめ

転載: www.cnblogs.com/Tassdar/p/10951444.html