GZip compression and changing UserAgent
Just in case you ever wanted to use the .NET Zip Library to receive GZip-compressed content from a webserver, as well as sending a custom UserAgent string to identify your app, here's how you do it:
/// <summary>
/// Used to send out a custom application UserAgent, and return a string from a GZip-compressed
/// response.
/// </summary>
/// <param name="strUrl">The url you want to retrieve.</param>
/// <returns></returns>
public string GetWebRequest(string strUrl)
{
StringBuilder mySB = new StringBuilder();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strUrl);
// Sends the HttpWebRequest and waits for the response.
request.UserAgent = "BSClient 0.3";
request.Headers.Add("Accept-Encoding", "gzip, deflate");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// Gets the stream associated with the response.
Stream receiveStream = GetGzipStream(response);
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
// Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader readStream = new StreamReader( receiveStream, encode );
Char[] read = new Char[256];
// Reads 256 characters at a time.
int count = readStream.Read( read, 0, 256 );
while (count > 0)
{
// Dumps the 256 characters on a string and displays the string to the console.
String str = new String(read, 0, count);
mySB.Append(str);
count = readStream.Read(read, 0, 256);
}
// Releases the resources of the response.
response.Close();
// Releases the resources of the Stream.
readStream.Close();
return mySB.ToString();
}
/// <summary>
/// Used in place of GetResponseStream(). This function will check out your HttpWebResponse's contents,
/// and return the proper string representation of the HttpWebResponse stream.
/// </summary>
/// <param name="response"></param>
/// <returns>String representation of the HttpWebResponse</returns>
private Stream GetGzipStream(HttpWebResponse response)
{
Stream compressedStream = null;
if (response.ContentEncoding=="gzip")
{
compressedStream = new GZipInputStream(response.GetResponseStream());
}
else if (response.ContentEncoding=="deflate")
{
compressedStream = new InflaterInputStream(response.GetResponseStream());
}
if (compressedStream != null)
{
MemoryStream decompressedStream = new MemoryStream();
int size = 2048;
byte[] writeData = new byte[2048];
while (true)
{
size = compressedStream.Read(writeData, 0, size);
if (size > 0)
{
decompressedStream.Write(writeData,0,size);
}
else
{
break;
}
}
decompressedStream.Seek(0, SeekOrigin.Begin);
return decompressedStream;
}
else
{
return response.GetResponseStream();
}
}