Пример #1
0
        static HttpClientFactory()
        {
            Get = (() =>
            {
                var httpClientHandler = new HttpClientHandler();
                if (httpClientHandler.SupportsAutomaticDecompression)
                {
                    httpClientHandler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
                }
                if (httpClientHandler.SupportsUseProxy())
                {
                    httpClientHandler.UseProxy = true;
                }
                if (httpClientHandler.SupportsAllowAutoRedirect())
                {
                    httpClientHandler.AllowAutoRedirect = true;
                }
                if (httpClientHandler.SupportsPreAuthenticate())
                {
                    httpClientHandler.PreAuthenticate = true;
                }

                var httpClient = new HttpClient(httpClientHandler);
                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/jsonp"));
                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/html"));

                return(httpClient);
            });
        }
        public ProxyModule()
        {
            var httpClientHandler = new HttpClientHandler();

            if (httpClientHandler.SupportsAutomaticDecompression)
            {
                httpClientHandler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
            }
            if (httpClientHandler.SupportsUseProxy())
            {
                httpClientHandler.UseProxy = true;
            }
            if (httpClientHandler.SupportsAllowAutoRedirect())
            {
                httpClientHandler.AllowAutoRedirect = true;
            }
            if (httpClientHandler.SupportsPreAuthenticate())
            {
                httpClientHandler.PreAuthenticate = true;
            }

            var httpClient = new HttpClient(httpClientHandler);

            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/jsonp"));
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/html"));
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("image/webp"));
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("image/*"));

            Get["/proxy", true] = async(parameters, ctx) =>
            {
                var parsedUrl      = ParseQueryString(Request.Query);
                var requestForType = parsedUrl.Item1;
                var url            = parsedUrl.Item2;

                var response = await httpClient.GetAsync(url);

                response.EnsureSuccessStatusCode();

                if (string.Equals(JsonFormat, requestForType, StringComparison.OrdinalIgnoreCase))
                {
                    return(await response.Content.ReadAsStringAsync().ConfigureAwait(false));
                }

                Stream responseStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);

                return(Response.FromStream(responseStream, DefaultImageResponseContentType));
            };

            Post["/proxy", true] = async(parameters, ctx) =>
            {
                var parsedUrl      = ParseQueryString(Request.Query);
                var requestForType = parsedUrl.Item1;
                var url            = parsedUrl.Item2.Split('?').FirstOrDefault();

                var         parametersToSend = parsedUrl.Item3;
                HttpContent content          = null;
                try
                {
                    content = new FormUrlEncodedContent(parametersToSend);
                }
                catch (FormatException fex)
                {
                    var tempContent = new MultipartFormDataContent();
                    foreach (var keyValuePair in parametersToSend)
                    {
                        tempContent.Add(new StringContent(keyValuePair.Value), keyValuePair.Key);
                    }
                    content = tempContent;
                }

                var response = await httpClient.PostAsync(url, content);

                response.EnsureSuccessStatusCode();

                if (string.Equals(JsonFormat, requestForType, StringComparison.OrdinalIgnoreCase))
                {
                    return(await response.Content.ReadAsStringAsync().ConfigureAwait(false));
                }

                Stream responseStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);

                return(Response.FromStream(responseStream, DefaultImageResponseContentType));
            };
        }
Пример #3
0
        async Task <IHttpTransferResult> _doDownload(DownloadQueueObject obj, IHttpTransferConfig downloadConfig)
        {
            // add support for Gzip decompression

            HttpClient httpClient;
            var        httpHandler = new HttpClientHandler();

            if (downloadConfig.Gzip)
            {
                if (httpHandler.SupportsAutomaticDecompression)
                {
                    httpHandler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
                }
            }

            if (downloadConfig.AllowRedirect && httpHandler.SupportsAllowAutoRedirect())
            {
                httpHandler.AllowAutoRedirect = true;
            }
            else
            {
                httpHandler.AllowAutoRedirect = false;
            }

            httpClient = new HttpClient(httpHandler);

            if (downloadConfig.Cookies != null)
            {
                var uri     = new Uri(obj.Url);
                var cookies = new CookieContainer();
                httpHandler.CookieContainer = cookies;

                foreach (var c in downloadConfig.Cookies)
                {
                    cookies.Add(uri, new Cookie(c.Key, c.Value));
                }
            }

            using (httpClient)
            {
                var method = HttpMethod.Get;

                switch (obj.Verb)
                {
                case "GET":
                    method = HttpMethod.Get;
                    break;

                case "POST":
                    method = HttpMethod.Post;
                    break;

                case "PUT":
                    method = HttpMethod.Put;
                    break;

                case "DELETE":
                    method = HttpMethod.Delete;
                    break;
                }

                using (var message = new HttpRequestMessage(method, obj.Url))
                {
                    if (downloadConfig.Headers != null)
                    {
                        foreach (var item in downloadConfig.Headers)
                        {
                            message.Headers.Add(item.Key, item.Value);
                        }
                    }

                    // Accept-Encoding:
                    if (downloadConfig.AcceptEncoding != null)
                    {
                        //message.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue(""));
                        message.Headers.Add("Accept-Encoding", downloadConfig.AcceptEncoding);
                    }


                    // Accept:
                    if (!string.IsNullOrWhiteSpace(downloadConfig.Accept))
                    {
                        message.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(downloadConfig.Accept));
                    }


                    if (!string.IsNullOrWhiteSpace(obj.Data))
                    {
                        var content = new StringContent(obj.Data, Encoding.UTF8,
                                                        downloadConfig.ContentEncoding ?? "application/json");
                        message.Content = content;
                    }

                    if (obj.ByteData != null)
                    {
                        var content = new ByteArrayContent(obj.ByteData, 0, obj.ByteData.Length);

                        message.Content = content;
                    }

                    if (downloadConfig.Auth != null && downloadConfig.AuthScheme != null)
                    {
                        message.Headers.Authorization = new AuthenticationHeaderValue(downloadConfig.AuthScheme,
                                                                                      downloadConfig.Auth);
                    }

                    if (downloadConfig.Timeout != 0)
                    {
                        httpClient.Timeout = TimeSpan.FromSeconds(downloadConfig.Timeout);
                    }

                    try
                    {
                        Debug.WriteLine("{0}: {1}", downloadConfig.Verb.ToLower() == "get" ? "Downloading": "Uploading", obj.Url);

                        using (var result = await httpClient.SendAsync(message))
                        {
                            Debug.WriteLine("Finished: {0}", obj.Url);
                            return(await _httpTransferService.GetResult(result, downloadConfig));
                        }
                    }
                    catch (HttpRequestException ex)
                    {
                        Debug.WriteLine("Warning - HttpRequestException encountered: {0}", ex.Message);

                        return(_httpTransferService.GetExceptionResult(ex,
                                                                       "XamlingCore.Portable.Net.Downloaders.HttpClientDownloader", downloadConfig));
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine("Warning - general HTTP exception encountered: {0}", ex.Message);
                        return(_httpTransferService.GetExceptionResult(ex,
                                                                       "XamlingCore.Portable.Net.Downloaders.HttpClientDownloader", downloadConfig));
                    }
                }
            }
        }