AcoustID.Web.WebHelper.SendPost C# (CSharp) Méthode

SendPost() public static méthode

public static SendPost ( string url, Stream body, bool useCompression ) : Task
url string
body System.IO.Stream
useCompression bool
Résultat Task
        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();
        }

Same methods

WebHelper::SendPost ( string url, string query, bool useCompression ) : Task

Usage Example

Exemple #1
0
 public static async Task <string> SendPost(string url, string query, bool useCompression)
 {
     using (var body = new MemoryStream(Encoding.Default.GetBytes(query)))
     {
         return(await WebHelper.SendPost(url, body, useCompression));
     }
 }
All Usage Examples Of AcoustID.Web.WebHelper::SendPost