CreateHttpClient() публичный статический Метод

public static CreateHttpClient ( bool automaticDecompression = true ) : HttpClient
automaticDecompression bool
Результат System.Net.Http.HttpClient
Пример #1
0
        public static async Task <string> SendPost(string url, Stream body, bool useCompression)
        {
            var client = WebHelper.CreateHttpClient();

            // The stream to hold the content bytes (gzipped or not).
            Stream stream;

            int size = (int)body.Length;

            useCompression &= (size > COMPRESSION_THRESHOLD);

            if (useCompression)
            {
                stream = new MemoryStream();

                // Create gzip stream.
                using (var gzip = new GZipStream(stream, CompressionMode.Compress, true))
                {
                    body.CopyTo(gzip);
                }

                // Reset stream position.
                stream.Seek(0L, SeekOrigin.Begin);
            }
            else
            {
                stream = body;
            }

            var content = new StreamContent(stream);

            content.Headers.ContentType   = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
            content.Headers.ContentLength = stream.Length;

            if (useCompression)
            {
                content.Headers.ContentEncoding.Add("gzip");
            }

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

            if (useCompression)
            {
                // Don't forget to dispose of the memory stream.
                stream.Dispose();
            }

            return(await response.Content.ReadAsStringAsync());
        }
Пример #2
0
        public static async Task <string> SendGet(string url, string query)
        {
            var client = WebHelper.CreateHttpClient();

            return(await client.GetStringAsync(url + "?" + query));
        }