/// create web client
/// set headers: User-Agent & Accept-Encoding
System.Net.WebClient wc;
wc = new System.Net.WebClient();
wc.Headers["User-Agent"] = "Mozilla/4.0"; // You must specify User-Agent type
wc.Headers["Accept-Encoding"] = "gzip, deflate"; // here we specify that our client supports both GZip and Deflate encoding types
/// request page from web server
System.IO.StreamReader webReader;
webReader = new System.IO.StreamReader(wc.OpenRead("http://www.know24.net/blog/"));
string data = string.Empty; // will be used to store our uncompressed page content
string sResponseHeader = wc.ResponseHeaders["Content-Encoding"]; // get response header
if (!string.IsNullOrEmpty(sResponseHeader))
{
if (sResponseHeader.ToLower().Contains("gzip"))
byte[] b = DecompressGzip(webReader.BaseStream);
data = System.Text.Encoding.GetEncoding(wc.Encoding.CodePage).GetString(b);
}
else if (sResponseHeader.ToLower().Contains("deflate"))
byte[] b = DecompressDeflate(webReader.BaseStream);
// uncompressed, standard response
else
data = webReader.ReadToEnd();
private static byte[] DecompressGzip(Stream streamInput)
Stream streamOutput = new MemoryStream();
int iOutputLength = 0;
try
byte[] readBuffer = new byte[4096];
/// read from input stream and write to gzip stream
using (GZipStream streamGZip = new GZipStream(streamInput, CompressionMode.Decompress))
int i;
while ((i = streamGZip.Read(readBuffer, 0, readBuffer.Length)) != 0)
streamOutput.Write(readBuffer, 0, i);
iOutputLength = iOutputLength + i;
catch (Exception ex)
// todo: handle exception
/// read uncompressed data from output stream into a byte array
byte[] buffer = new byte[iOutputLength];
streamOutput.Position = 0;
streamOutput.Read(buffer, 0, buffer.Length);
return buffer;
Remember Me
Page rendered at Monday, February 06, 2012 11:41:19 PM (South Africa Standard Time, UTC+02:00)
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.