The GZipHttpWebResponse.cs file does response processing. To see the GZipHttpWebResponse.cs file un-annotated, click here.
Reference the needed namespaces. |
using System; |
using System.Web; | |
using System.IO; | |
using System.Net; | |
using System.Runtime.Serialization; | |
using System.Xml; | |
|
|
|
// #ZipLib was developed by Mike Krueger, and is available under the GNU Public License at: |
|
//http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx |
Specify the namespace for this class. |
using ICSharpCode.SharpZipLib.GZip; |
| |
namespace Galileo.Web | |
|
{ |
|
/// <summary> |
|
/// HttpWebResponse with gzip decompression |
|
/// The source for this class is based on this article on dotnetjunkies.com: |
|
///http://www.dotnetjunkies.com/Tutorial/46630AE2-1C79-4D5F-827E-6C2857FF1D23.dcik |
|
/// </summary> |
|
[Serializable] |
Constructor. Take what we need from the response parameter, decompressing the response stream if it is compressed. |
public class GZipHttpWebResponse : WebResponse |
|
{ |
Declare member variables. |
private Stream m_decompressedStream; |
private string _contentType; | |
private long _contentLength; | |
private string _contentEncoding; | |
private Uri _responseUri; | |
private WebHeaderCollection _httpResponseHeaders; | |
|
|
|
///// <summary> |
|
///// default constructor from WebResponse |
|
///// </summary> |
|
///// <param name="serializationInfo"></param> |
|
///// <param name="streamingContext"></param> |
|
//protected GZipHttpWebResponse( |
|
//SerializationInfo serializationInfo, |
|
//StreamingContext streamingContext |
|
//) : base (serializationInfo, streamingContext) |
|
//{ |
|
//} |
|
// |
Override base class behavior to use our member variables for these properties. |
public override long ContentLength |
{ | |
set | |
{ | |
_contentLength = value; | |
} | |
| |
get | |
{ | |
return _contentLength; | |
} | |
} | |
| |
| |
public override string ContentType | |
{ | |
set | |
{ | |
_contentType = value; | |
} | |
| |
get | |
{ | |
return _contentType; | |
} | |
} | |
| |
public override WebHeaderCollection Headers | |
{ | |
get | |
{ | |
return _httpResponseHeaders; | |
} | |
} | |
| |
public string ContentEncoding | |
{ | |
set | |
{ | |
_contentEncoding = value;; | |
} | |
| |
get | |
{ | |
return _contentEncoding; | |
} | |
} | |
|
|
|
public GZipHttpWebResponse(HttpWebResponse response) |
|
{ |
|
try |
|
{ |
Initialize our members variables from the HttpWebResponse object. |
_contentType = response.ContentType; |
_contentLength = response.ContentLength; | |
_contentEncoding = response.ContentEncoding; | |
_responseUri = response.ResponseUri; | |
_httpResponseHeaders = response.Headers; | |
If the response is compressed, then decompress it into a memory stream. |
if (_contentEncoding=="gzip") |
{ | |
// Decompress response stream with GZip | |
const int BUF_SIZE = 16384; | |
Stream compressedStream = new GZipInputStream(response.GetResponseStream(), BUF_SIZE); | |
m_decompressedStream = new MemoryStream(); | |
| |
byte[] writeData = new byte[BUF_SIZE]; | |
int size = compressedStream.Read(writeData, 0, BUF_SIZE); | |
while (size > 0) | |
{ | |
m_decompressedStream.Write(writeData, 0, size); | |
size = compressedStream.Read(writeData, 0, BUF_SIZE); | |
} | |
m_decompressedStream.Seek(0, SeekOrigin.Begin); | |
_contentEncoding = ""; | |
_httpResponseHeaders.Remove("Content-Encoding"); | |
_contentLength = m_decompressedStream.Length; | |
// We have decompressed the entire response, so we no longer need the compressed version. | |
response.Close(); | |
} | |
If the response is not compressed, just... |
else |
{ | |
...return the response stream from the HttpWebResponse object. |
m_decompressedStream = response.GetResponseStream(); |
} | |
|
} |
If an exception occurs, clean up the resources we are using and re-throw the exception. |
catch(Exception) |
{ | |
// We got an exception processing the response. Clean up. | |
response.Close(); | |
Close(); | |
throw; | |
|
} |
|
} |
|
|
Make sure to close the decompressed stream when the object is deleted. |
~GZipHttpWebResponse() |
{ | |
Close(); | |
} | |
|
|
Return the stream referenced by our local member variable. |
public override Stream GetResponseStream() |
{ | |
return m_decompressedStream; | |
|
} |
|
|
|
public override void Close() |
|
{ |
If our member variable is pointing to a stream, close it. |
if(m_decompressedStream != null) |
{ | |
m_decompressedStream.Close(); | |
m_decompressedStream = null; | |
} | |
} | |
|
|
Override base class behavior to use our member variables for these properties. |
public override Uri ResponseUri { get { return _responseUri; } } |
|
} |
|
} |
|
|