Exemple #1
0
        public async Task StringContent_ReadAsString()
        {
            const int contentLength = 100;

            int[] expectedPercentages = { 100 };

            List <int> actualPercentages = new List <int>();

            string expectedContent = new String('x', contentLength);

            using (StringContent stringContent = new StringContent(expectedContent))
                using (ProgressContent progressContent = new ProgressContent(stringContent))
                {
                    progressContent.Progress.Percentage(10).Subscribe(progress =>
                    {
                        actualPercentages.Add(progress.PercentComplete);
                    });

                    string actualContent = await progressContent.ReadAsStringAsync();

                    Assert.Equal(expectedContent, actualContent);

                    Assert.Equal(expectedPercentages.Length, actualPercentages.Count);
                    Assert.Equal(expectedPercentages, actualPercentages);
                }
        }
        private async void ShowProgress()
        {
            var lockDialog  = new CustomDialog();
            var dataContext = new ProgressContent(x =>
            {
                _dialogCoordinator.HideMetroDialogAsync(this, lockDialog);
            });

            lockDialog.Content = new ProgressControl {
                DataContext = dataContext
            };
            await _dialogCoordinator.ShowMetroDialogAsync(this, lockDialog);
        }
Exemple #3
0
        /// <summary>
        ///     Get the request's sequence of raw progress data.
        /// </summary>
        /// <param name="request">
        ///     The HTTP request message.
        /// </param>
        /// <returns>
        ///     An observable sequence of raw progress data.
        /// </returns>
        public static IObservable <RawProgressData <long> > GetProgress(this HttpRequestMessage request)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            ProgressContent progressContent = request.Content as ProgressContent;

            if (progressContent == null)
            {
                return(null);
            }

            return(progressContent.Progress);
        }
Exemple #4
0
        /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
        /// <summary>上传任务拍照临时文件</summary>
        /// <exception cref="ApiException">A server side error occurred.</exception>
        /// <param name="file"></param>
        public async System.Threading.Tasks.Task <Tourism.STD.Models.InfoModel <NetCore.TempFileInfo> > UploadForTaskTempFileForProgressAsync(FileParameter file, System.Threading.CancellationToken cancellationToken)
        {
            var urlBuilder_ = new System.Text.StringBuilder();

            urlBuilder_.Append("PhoneApi/SceniceMap/UploadForTaskTempFile");

            var client_        = _httpClient;
            var disposeClient_ = false;

            try
            {
                using (var request_ = await CreateHttpRequestMessageAsync(cancellationToken).ConfigureAwait(false))
                {
                    var boundary_ = System.Guid.NewGuid().ToString();
                    var content_  = new System.Net.Http.MultipartFormDataContent(boundary_);
                    content_.Headers.Remove("Content-Type");
                    content_.Headers.TryAddWithoutValidation("Content-Type", "multipart/form-data; boundary=" + boundary_);
                    if (file != null)
                    {
                        var content_file_ = new ProgressContent(file.Data, CacheBufferLengthForProgress);
                        var methonInfo    = MethodBase.GetCurrentMethod();
                        content_file_.Progress += (arg, arg1) =>
                        {
                            InvokeProgressEvent(new EventArgsOfClientProgress(
                                                    "file",
                                                    methonInfo,
                                                    arg1));
                        };
                        if (!string.IsNullOrEmpty(file.ContentType))
                        {
                            content_file_.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse(file.ContentType);
                        }
                        content_.Add(content_file_, "file", file.FileName ?? "file");
                    }
                    request_.Content = content_;
                    request_.Method  = new System.Net.Http.HttpMethod("POST");
                    request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("application/json"));

                    PrepareRequest(client_, request_, urlBuilder_);
                    var url_ = urlBuilder_.ToString();
                    request_.RequestUri = new System.Uri(url_, System.UriKind.RelativeOrAbsolute);
                    PrepareRequest(client_, request_, url_);

                    var response_ = await client_.SendAsync(request_, System.Net.Http.HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);

                    var disposeResponse_ = true;
                    try
                    {
                        var headers_ = System.Linq.Enumerable.ToDictionary(response_.Headers, h_ => h_.Key, h_ => h_.Value);
                        if (response_.Content != null && response_.Content.Headers != null)
                        {
                            foreach (var item_ in response_.Content.Headers)
                            {
                                headers_[item_.Key] = item_.Value;
                            }
                        }

                        ProcessResponse(client_, response_);

                        var status_ = (int)response_.StatusCode;
                        if (status_ == 200)
                        {
                            var objectResponse_ = await ReadObjectResponseAsync <Tourism.STD.Models.InfoModel <NetCore.TempFileInfo> >(response_, headers_).ConfigureAwait(false);

                            return(objectResponse_.Object);
                        }
                        else
                        {
                            var responseData_ = response_.Content == null ? null : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);

                            throw new ApiException("The HTTP status code of the response was not expected (" + status_ + ").", status_, responseData_, headers_, null);
                        }
                    }
                    finally
                    {
                        if (disposeResponse_)
                        {
                            response_.Dispose();
                        }
                    }
                }
            }
            finally
            {
                if (disposeClient_)
                {
                    client_.Dispose();
                }
            }
        }