public static async Task <PushHttpResponse> RequestAsync(PushHttpRequest pushRequest)
        {
            using (var httpClientHandler = new HttpClientHandler())
            {
#if NETSTANDARD16
                httpClientHandler.MaxConnectionsPerServer = 100;
#endif
                httpClientHandler.UseProxy = false;

                using (var client = new HttpClient(httpClientHandler))
                {
                    client.DefaultRequestHeaders.ExpectContinue = false;

                    var request = new HttpRequestMessage
                    {
                        RequestUri = new Uri(pushRequest.Url),
                        Method     = pushRequest.HttpMethod,
                    };

                    if (!string.IsNullOrEmpty(pushRequest.Body))
                    {
                        request.Content = new ByteArrayContent(pushRequest.Encoding.GetBytes(pushRequest.Body));
                    }

                    foreach (var headerKey in pushRequest.Headers.AllKeys)
                    {
                        request.Headers.Add(headerKey, pushRequest.Headers[headerKey]);
                    }

                    var response = await client.SendAsync(request);

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

                    var responseEncoding = Encoding.ASCII;
                    try
                    {
                        responseEncoding = Encoding.GetEncoding(response.Content.Headers.ContentEncoding.FirstOrDefault());
                    }
                    catch
                    {
                    }

                    var responseHeaders = new WebHeaderCollection();
                    foreach (var header in response.Headers)
                    {
                        responseHeaders[header.Key] = header.Value.FirstOrDefault();
                    }

                    return(new PushHttpResponse
                    {
                        Body = responseBody,
                        Headers = responseHeaders,
                        Uri = response.RequestMessage.RequestUri,
                        Encoding = responseEncoding,
                        LastModified = response.Content.Headers.LastModified.GetValueOrDefault().DateTime,
                        StatusCode = response.StatusCode
                    });
                }
            }
        }
        public static async Task <PushHttpResponse> RequestAsync(PushHttpRequest request)
        {
            var httpRequest = HttpWebRequest.CreateHttp(request.Url);

            httpRequest.Proxy = null;

            httpRequest.Headers = request.Headers;

            if (!string.IsNullOrEmpty(request.Body))
            {
                var requestStream = await httpRequest.GetRequestStreamAsync();

                var requestBody = request.Encoding.GetBytes(request.Body);

                await requestStream.WriteAsync(requestBody, 0, requestBody.Length);
            }

            HttpWebResponse httpResponse;
            Stream          responseStream;

            try {
                httpResponse = await httpRequest.GetResponseAsync() as HttpWebResponse;

                responseStream = httpResponse.GetResponseStream();
            } catch (WebException webEx) {
                httpResponse = webEx.Response as HttpWebResponse;

                responseStream = httpResponse.GetResponseStream();
            }

            var body = string.Empty;

            using (var sr = new StreamReader(responseStream))
                body = await sr.ReadToEndAsync();

            var responseEncoding = Encoding.ASCII;

            try {
                responseEncoding = Encoding.GetEncoding(httpResponse.ContentEncoding);
            } catch {
            }

            var response = new PushHttpResponse {
                Body         = body,
                Headers      = httpResponse.Headers,
                Uri          = httpResponse.ResponseUri,
                Encoding     = responseEncoding,
                LastModified = httpResponse.LastModified,
                StatusCode   = httpResponse.StatusCode
            };

            httpResponse.Close();
            httpResponse.Dispose();

            return(response);
        }
Esempio n. 3
0
        public static async Task<PushHttpResponse> RequestAsync (PushHttpRequest request)
        {
            var httpRequest = HttpWebRequest.CreateHttp (request.Url);
            httpRequest.Proxy = null;

            httpRequest.Headers = request.Headers;

            if (!string.IsNullOrEmpty (request.Body)) {
                var requestStream = await httpRequest.GetRequestStreamAsync ();

                var requestBody = request.Encoding.GetBytes (request.Body);

                await requestStream.WriteAsync (requestBody, 0, requestBody.Length);
            }

            HttpWebResponse httpResponse;
            Stream responseStream;

            try {
                httpResponse = await httpRequest.GetResponseAsync () as HttpWebResponse;

                responseStream = httpResponse.GetResponseStream ();
            } catch (WebException webEx) {
                httpResponse = webEx.Response as HttpWebResponse;

                responseStream = httpResponse.GetResponseStream ();
            }

            var body = string.Empty;

            using (var sr = new StreamReader (responseStream))
                body = await sr.ReadToEndAsync ();
                
            var responseEncoding = Encoding.ASCII;
            try {
                responseEncoding = Encoding.GetEncoding (httpResponse.ContentEncoding);
            } catch {
            }

            var response = new PushHttpResponse {
                Body = body,
                Headers = httpResponse.Headers,
                Uri = httpResponse.ResponseUri,
                Encoding = responseEncoding,
                LastModified = httpResponse.LastModified,
                StatusCode = httpResponse.StatusCode
            };

            httpResponse.Close ();
            httpResponse.Dispose ();

            return response;
        }
Esempio n. 4
0
        public static async Task <PushHttpResponse> RequestAsync(PushHttpRequest request)
        {
#if NETSTANDARD
            WinHttpHandler httpHandler = new WinHttpHandler();
            httpHandler.SslProtocols = SslProtocols.Tls12;
            HttpClient client = new HttpClient(httpHandler);

            client.DefaultRequestHeaders.Clear();
            foreach (var headerKey in request.Headers.AllKeys)
            {
                client.DefaultRequestHeaders.TryAddWithoutValidation(headerKey, request.Headers[headerKey]);
            }

            HttpMethod method = HttpMethod.Get;
            switch (request.Method)
            {
            case "GET":
                method = HttpMethod.Get;
                break;

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

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

            HttpRequestMessage message = new HttpRequestMessage(method, request.Url);
            HttpContent        content = new StringContent(request.Body, request.Encoding);
            message.Content = content;

            HttpResponseMessage httpResponse = await client.SendAsync(message);

            WebHeaderCollection responseHeaderCollection = new WebHeaderCollection();
            foreach (var responseHeader in httpResponse.Headers)
            {
                responseHeaderCollection[responseHeader.Key] = responseHeader.Value.FirstOrDefault();
            }

            PushHttpResponse response = new PushHttpResponse
            {
                Body         = await httpResponse.Content.ReadAsStringAsync(),
                Headers      = responseHeaderCollection,
                Uri          = httpResponse.RequestMessage.RequestUri,
                Encoding     = Encoding.GetEncoding(httpResponse.Content.Headers.ContentEncoding.First()),
                LastModified = httpResponse.Content.Headers.LastModified == null ? DateTime.Now : httpResponse.Content.Headers.LastModified.Value.DateTime,
                StatusCode   = httpResponse.StatusCode
            };

            client.Dispose();
            return(response);
#else
            var httpRequest = HttpWebRequest.CreateHttp(request.Url);
            httpRequest.Proxy = null;

            httpRequest.Headers = request.Headers;

            if (!string.IsNullOrEmpty(request.Body))
            {
                var requestStream = await httpRequest.GetRequestStreamAsync();

                var requestBody = request.Encoding.GetBytes(request.Body);

                await requestStream.WriteAsync(requestBody, 0, requestBody.Length);
            }

            HttpWebResponse httpResponse;
            Stream          responseStream;

            try {
                httpResponse = await httpRequest.GetResponseAsync() as HttpWebResponse;

                responseStream = httpResponse.GetResponseStream();
            } catch (WebException webEx) {
                httpResponse = webEx.Response as HttpWebResponse;

                responseStream = httpResponse.GetResponseStream();
            }

            var body = string.Empty;

            using (var sr = new StreamReader(responseStream))
                body = await sr.ReadToEndAsync();

            var responseEncoding = Encoding.ASCII;
            try {
                responseEncoding = Encoding.GetEncoding(httpResponse.ContentEncoding);
            } catch {
            }

            var response = new PushHttpResponse {
                Body         = body,
                Headers      = httpResponse.Headers,
                Uri          = httpResponse.ResponseUri,
                Encoding     = responseEncoding,
                LastModified = httpResponse.LastModified,
                StatusCode   = httpResponse.StatusCode
            };

            httpResponse.Close();
            httpResponse.Dispose();

            return(response);
#endif
        }