コード例 #1
0
        public static VkUploadVideoResponse FromJson(JToken json)
        {
            if (json == null)
            {
                throw new ArgumentNullException("json");
            }

            var result = new VkUploadVideoResponse();

            if (json["video_hash"] != null)
            {
                result.VideoHash = (string)json["video_hash"];
            }

            if (json["size"] != null)
            {
                result.Size = (long)json["size"];
            }

            if (json["video_id"] != null)
            {
                result.VideoId = (long)json["video_id"];
            }

            return(result);
        }
コード例 #2
0
        public async Task <VkUploadVideoResponse> Upload(string url, string fileName, Stream stream)
        {
            var client = new HttpClient();

            string boundary = "----------" + DateTime.Now.Ticks.ToString("x");
            var    content  = new MultipartFormDataContent(boundary);

            var fileContent = new StreamContent(stream);

            fileContent.Headers.ContentType        = new MediaTypeHeaderValue("application/octet-stream");
            fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
            {
                FileName = "\"" + fileName + "\"",
                Name     = "\"video_file\""
            };

            content.Add(fileContent);

            var responseMessage = await client.PostAsync(new Uri(url), content);

            byte[] bytes = await responseMessage.Content.ReadAsByteArrayAsync();

            Encoding encoding = Encoding.UTF8; //VK return windows-1251 which causes exception on Win10
            string   response = encoding.GetString(bytes, 0, bytes.Length);

            Debug.WriteLine("Upload response: " + response);

            var json = JObject.Parse(response);

            return(VkUploadVideoResponse.FromJson(json));
        }