Пример #1
0
        /// <summary>
        /// Builds the HTTP client.
        /// </summary>
        /// <param name="proxyUri">The proxy URI.</param>
        /// <param name="proxyCredentials">The proxy credentials.</param>
        /// <param name="enableDefaultSystemAuthentication">if set to <c>true</c> [enable default system authentication].</param>
        /// <param name="requestHeaders">The request headers to add to all requests.</param>
        private void BuildHttpClient(string proxyUri, ICredentials proxyCredentials, bool enableDefaultSystemAuthentication, Dictionary <string, string> requestHeaders)
        {
            HttpClientHandler httpClientHandler = HttpClientHandlerBuilderNew.Build(proxyUri, proxyCredentials, enableDefaultSystemAuthentication);

            this.proxy      = httpClientHandler.Proxy;
            this.httpClient = new HttpClient(httpClientHandler);

            AddRequestHeaders(httpClient, requestHeaders);
        }
Пример #2
0
        /// <summary>
        /// Uploads a file using a blob reference.
        /// </summary>
        /// <param name="url"></param>
        /// <param name="proxyUri"></param>
        /// <param name="credentials"></param>
        /// <param name="timeout"></param>
        /// <param name="enableDefaultSystemAuthentication"></param>
        /// <param name="blobId"></param>
        /// <param name="stream"></param>
        /// <param name="totalLength"></param>
        /// <param name="progressIndicator"></param>
        /// <param name="cancellationToken"></param>
        /// <param name="useCancellationToken"></param>
        /// <returns></returns>
        internal async Task <HttpResponseMessage> UploadAsync(String url, String proxyUri, ICredentials credentials,
                                                              int timeout, bool enableDefaultSystemAuthentication, String blobId, Stream stream, long totalLength, AceQLProgressIndicator progressIndicator, CancellationToken cancellationToken, bool useCancellationToken)
        {
            HttpClientHandler      handler = HttpClientHandlerBuilderNew.Build(proxyUri, credentials, enableDefaultSystemAuthentication);
            ProgressMessageHandler processMessageHander = new ProgressMessageHandler(handler);
            HttpClient             httpClient           = new HttpClient(processMessageHander);

            if (timeout != 0)
            {
                long nanoseconds = 1000000 * timeout;
                httpClient.Timeout = new TimeSpan(nanoseconds / 100);
            }

            processMessageHander.HttpSendProgress += (sender, e) =>
            {
                int num = e.ProgressPercentage;
                this.tempLen += e.BytesTransferred;

                if (progressIndicator != null)
                {
                    if (totalLength > 0 && tempLen > totalLength / 100)
                    {
                        tempLen = 0;
                    }
                    int cpt = progressIndicator.Percent;
                    cpt++;
                    progressIndicator.SetValue(Math.Min(99, cpt));

                    if (DEBUG)
                    {
                        ConsoleEmul.WriteLine(DateTime.Now + " progressHolder.Progress: " + progressIndicator.Percent);
                    }
                }
                else
                {
                    if (DEBUG)
                    {
                        ConsoleEmul.WriteLine(DateTime.Now + " num: " + num);
                    }
                }
            };

            StringContent stringContentBlobId = new StringContent(blobId);

            try
            {
                var multipart = new MultipartFormDataContent();
                multipart.Add(stringContentBlobId, '"' + "blob_id" + '"');
                multipart.Add(new StreamContent(stream), '"' + "file" + '"', '"' + blobId + '"');

                if (DEBUG)
                {
                    ConsoleEmul.WriteLine();
                    ConsoleEmul.WriteLine("url     : " + url);
                    ConsoleEmul.WriteLine("blob_id : " + blobId);
                }

                HttpResponseMessage response = null;

                if (!useCancellationToken)
                {
                    response = await httpClient.PostAsync(url, multipart).ConfigureAwait(false);
                }
                else
                {
                    response = await httpClient.PostAsync(url, multipart, cancellationToken).ConfigureAwait(false);
                }

                // Allows a retry for 407, because can happen time to time with Web proxies
                if (response.StatusCode.Equals(HttpStatusCode.ProxyAuthenticationRequired))
                {
                    while (proxyAuthenticationCallCount < HttpRetryManager.ProxyAuthenticationCallLimit)
                    {
                        proxyAuthenticationCallCount++;
                        if (!useCancellationToken)
                        {
                            response = await httpClient.PostAsync(url, multipart).ConfigureAwait(false);
                        }
                        else
                        {
                            response = await httpClient.PostAsync(url, multipart, cancellationToken).ConfigureAwait(false);
                        }

                        if (!response.StatusCode.Equals(HttpStatusCode.ProxyAuthenticationRequired))
                        {
                            proxyAuthenticationCallCount = 0;
                            break;
                        }
                    }
                }

                if (progressIndicator != null)
                {
                    progressIndicator.SetValue(100);
                }

                return(response);
            }
            finally
            {
                stream.Dispose();
                httpClient.Dispose();
            }
        }