Exemple #1
0
        private HttpMessageHandler CreateMessageHandler(ODataRequest request)
        {
            if (_session.Settings.OnCreateMessageHandler != null)
            {
                return(_session.Settings.OnCreateMessageHandler());
            }
            else
            {
                var clientHandler = new HttpClientHandler();

                // Perform this test to prevent failure to access Credentials/PreAuthenticate properties on SL5
                if (request.Credentials != null)
                {
                    clientHandler.Credentials = request.Credentials;
                    if (clientHandler.SupportsPreAuthenticate())
                    {
                        clientHandler.PreAuthenticate = true;
                    }
                }

                if (_session.Settings.OnApplyClientHandler != null)
                {
                    _session.Settings.OnApplyClientHandler(clientHandler);
                }

                return(clientHandler);
            }
        }
Exemple #2
0
        private static HttpMessageHandler CreateMessageHandler(ODataClientSettings settings)
        {
            if (settings.OnCreateMessageHandler != null)
            {
                return(settings.OnCreateMessageHandler());
            }
            else
            {
                var clientHandler = new HttpClientHandler();

                // Perform this test to prevent failure to access Credentials/PreAuthenticate properties on SL5
                if (settings.Credentials != null)
                {
                    clientHandler.Credentials = settings.Credentials;
                    if (Utils.IsDesktopPlatform() || clientHandler.SupportsPreAuthenticate())
                    {
                        clientHandler.PreAuthenticate = true;
                    }
                }

                if (settings.OnApplyClientHandler != null)
                {
                    settings.OnApplyClientHandler(clientHandler);
                }

                return(clientHandler);
            }
        }
        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);
            });
        }
Exemple #4
0
        /// <summary>
        /// Calls the Payoff service method
        /// </summary>
        public static async Task <string> Payoff(ResolverData data)
        {
            var handler = new HttpClientHandler();

            handler.PreAuthenticate = handler.SupportsPreAuthenticate();

            using (var client = new HttpClient(handler))
            {
                try
                {
                    // formulate full url
                    string url = string.Format(_payoffPathV2, data.Host, data.Payload, data.Type);
                    AppendClientData(ref url, data);

                    var now = DateTime.UtcNow;
                    client.DefaultRequestHeaders.Date = now;
                    string body    = SerializeObject(CreateMetricV2()).ToString();
                    int    lenBody = Encoding.UTF8.GetByteCount(body);
                    var    content = new StringContent(body);
                    content.Headers.ContentType = MediaTypeHeaderValue.Parse(_contentType);

                    // set user credentials in Authorization header
                    string authInfo = CreateAuthorizationHeader(url, "POST", lenBody, _contentType, now);
                    client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", authInfo);

                    // make request
                    HttpResponseMessage response;
                    try
                    {
                        Elapsed.Restart();
                        response = await client.PostAsync(url, content);
                    }
                    finally { Elapsed.Stop(); }

                    StatusCode = response.StatusCode;
                    if (!response.IsSuccessStatusCode)
                    {
                        return("StatusCode: " + StatusCode);
                    }

                    var result = response.Content.ReadAsStringAsync().Result;
                    return(JsonIO.FormatFragment(result));
                }
                catch (Exception e)
                {
                    var error = string.Format("Exception: " + e.Message);
                    if (e.InnerException != null)
                    {
                        error += string.Format("\nInner Exception: " + e.InnerException.Message);
                    }

                    return(error);
                }
            }
        }
        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));
            };
        }