Get Request Stream with GZip Stream and Deflate Stream

[C#] In this example code will show you the code "how to request with GZipStream and DeflateStream"





public static string Request(string UriObj, string Method = "GET", string data = "", string token = "")
        {
            string _result = string.Empty;          

            lock (_logrequest)
            {
                StreamReader sr = null;
                HttpWebResponse response = null;
                Stream stream = null;
                HttpWebRequest request = null;
                try
                {
                    request = (HttpWebRequest)WebRequest.Create(UriObj);
                    request.Method = Method;
                    request.Timeout = 40000;                
                    request.ContentType = "application/x-www-form-urlencoded";
                    if (!string.IsNullOrEmpty(token))
                    {
                        request.Headers.Add("Authorization", token);
                    }
                    request.Headers.Add("Accept-Encoding", "gzip,deflate");                  
                    if (!string.IsNullOrEmpty(data))
                    {
                        var writer = new StreamWriter(request.GetRequestStream());
                        writer.Write(data);
                        writer.Close();
                    }
                    ServicePointManager.ServerCertificateValidationCallback = delegate(object s, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors) { return true; };

                    response = (HttpWebResponse)request.GetResponse();

                    switch (response.ContentEncoding.ToUpperInvariant())
                    {
                        case "GZIP":
                            stream = new System.IO.Compression.GZipStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress);
                            break;
                        case "DEFLATE":
                            stream = new System.IO.Compression.DeflateStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress);
                            break;
                        default:
                            stream = response.GetResponseStream();
                            break;
                    }

                    sr = new StreamReader(stream, true);
                    _result = sr.ReadToEnd(); 
                }
                catch (WebException ex)
                { 
                }
                catch (TimeoutException ex)
                {
                }
                catch (Exception ex)
                {                    
                }
                finally
                {
                    if (request != null)
                        request.Abort();
                    if (sr != null)
                        sr.Close();
                    if (response != null)
                        response.Close();
                    if (stream != null)
                        stream.Close();
                }
            }           
            return _result.Replace("\r\n", "");
        }
Reactions

Post a Comment

0 Comments