public static string GetResponseText(this HttpWebResponse instance, Encoding contentEncoding = null) { Contract.Requires(instance != null); var resStream = instance.GetResponseStream(); if (instance.ContentEncoding.Equals("gzip", StringComparison.OrdinalIgnoreCase)) { var mem = new MemoryStream(); using (var gzipStream = new GZipStream(resStream, CompressionMode.Decompress, true)) { gzipStream.FixedCopyTo(mem); } mem.Position = 0L; resStream = mem; } using (var reader = new StreamReader(resStream, contentEncoding ?? instance.GetContentEncoding())) { return(reader.ReadToEnd()); } }