public static async Task <string> UploadVideoAsync(string name, string description, VideoPrivacyType privacyType,
                                                           byte[] bytes)
        {
            string videoId = null;

            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key",
                                                 Common.CoreConstants.VideoIndexerSubscriptionKey);

                var uri = new Uri(
                    $"{CogsExplorer.Common.CoreConstants.VideoIndexerBaseUrl}?name={name}&description={description}&privacy={privacyType}");

                try
                {
                    var payload = new HttpMultipartContent {
                        new HttpBufferContent(bytes.AsBuffer())
                    };

                    var response = await client.PostAsync(uri, payload);

                    var result = await response.Content.ReadAsStringAsync();

                    dynamic videoIdResult = JsonConvert.DeserializeObject(result);

                    videoId = videoIdResult.ToString();
                }
                catch (Exception ex)
                {
                }
            }

            return(videoId);
        }
Exemple #2
0
        public async static Task <CallRet> MultiPost(string url, NameValueCollection formData, Stream inputStream, IWebProxy proxy = null)
        {
            string boundary = RandomBoundary();

            HttpClient           client  = new HttpClient();
            HttpMultipartContent content = new HttpMultipartContent();

            content.Headers.Add("Content-Type", "multipart/form-data; boundary=" + boundary);

            //WebRequest webRequest = WebRequest.Create(url);

            //if (proxy != null)
            //{
            //    webRequest.Proxy = proxy;
            //}

            //webRequest.Method = "POST";
            //webRequest.ContentType = "multipart/form-data; boundary=" + boundary;

            Stream postDataStream = GetPostStream(inputStream, formData["key"], formData, boundary);

            content.Add(new HttpStreamContent(postDataStream.AsInputStream()));

            //webRequest.ContentLength = postDataStream.Length;
            //Stream reqStream = webRequest.GetRequestStream();
            //postDataStream.Position = 0;

            //byte[] buffer = new byte[1024];
            //int bytesRead = 0;

            //while ((bytesRead = postDataStream.Read(buffer, 0, buffer.Length)) != 0)
            //{
            //    reqStream.Write(buffer, 0, bytesRead);
            //}
            //postDataStream.Dispose();
            //reqStream.Dispose();

            try
            {
                var resp = await client.PostAsync(new Uri(url), content);

                return(await RPC.Client.HandleResult(resp));
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
                return(new CallRet(Windows.Web.Http.HttpStatusCode.BadRequest, e));
            }
        }
        public virtual async Task <HttpRequestMessage> SendRequest(Stream stream)
        {
            var content = new HttpMultipartContent();

            content.Headers.ContentType = HttpMediaTypeHeaderValue.Parse(String.Format(CultureInfo.InvariantCulture,
                                                                                       @"multipart/related; boundary=""{0}""; type = ""application/atom+xml""",
                                                                                       _boundary));
            content.Add(new HttpStreamContent(stream.AsInputStream()));

            _request.Content = content;

            return(_request);

            // _request.ContentLength = _requestBodyTop.Length + stream.Length + _requestBodyBottom.Length;
            //_request.AllowWriteStreamBuffering = false;
            //_requestStream = await _request.GetRequestStreamAsync();
            //_requestStream.Write(_requestBodyTop.ToArray(), 0, (int)_requestBodyTop.Length);
            //await StreamHelper.TransferAsync(stream, _requestStream, 8192, true);
            //_requestStream.Write(_requestBodyBottom.ToArray(), 0, (int)_requestBodyBottom.Length);
            //await _requestStream.FlushAsync();
            //return _request;
        }
Exemple #4
0
        /// <summary>
        /// Adds a MultipartContent to the request.
        /// </summary>
        /// <param name="subtype">The sub type if needed.</param>
        /// <param name="boundary">The boundary if needed.</param>
        /// <returns>this.</returns>
        public static T AddMultipart <T>(this T request, string subtype = "", string boundary = "")
            where T : RestRequest
        {
#if UNIVERSAL
            IHttpContent content = null;

            if (String.IsNullOrEmpty(subtype))
            {
                content = new HttpMultipartContent();
            }
            else if (String.IsNullOrEmpty(boundary))
            {
                content = new HttpMultipartContent(subtype);
            }
            else
            {
                content = new HttpMultipartContent(subtype, boundary);
            }
#else
            HttpContent content = null;

            if (String.IsNullOrEmpty(subtype))
            {
                content = new MultipartContent();
            }
            else if (String.IsNullOrEmpty(boundary))
            {
                content = new MultipartContent(subtype);
            }
            else
            {
                content = new MultipartContent(subtype, boundary);
            }
#endif
            return(request.AddContent(content));
        }
Exemple #5
0
        public async static Task<CallRet> MultiPost(string url, NameValueCollection formData, Stream inputStream, IWebProxy proxy = null)
        {
            string boundary = RandomBoundary();

            HttpClient client = new HttpClient();
            HttpMultipartContent content = new HttpMultipartContent();
            content.Headers.Add("Content-Type", "multipart/form-data; boundary=" + boundary);

            //WebRequest webRequest = WebRequest.Create(url);

            //if (proxy != null)
            //{
            //    webRequest.Proxy = proxy;
            //}

            //webRequest.Method = "POST";
            //webRequest.ContentType = "multipart/form-data; boundary=" + boundary;

            Stream postDataStream = GetPostStream(inputStream, formData["key"], formData, boundary);
            content.Add(new HttpStreamContent(postDataStream.AsInputStream()));

            //webRequest.ContentLength = postDataStream.Length;
            //Stream reqStream = webRequest.GetRequestStream();
            //postDataStream.Position = 0;

            //byte[] buffer = new byte[1024];
            //int bytesRead = 0;

            //while ((bytesRead = postDataStream.Read(buffer, 0, buffer.Length)) != 0)
            //{
            //    reqStream.Write(buffer, 0, bytesRead);
            //}
            //postDataStream.Dispose();
            //reqStream.Dispose();

            try
            {
                var resp = await client.PostAsync(new Uri(url), content);
                return await RPC.Client.HandleResult(resp);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
                return new CallRet(Windows.Web.Http.HttpStatusCode.BadRequest, e);
            }
        }