Friday, October 24, 2008

How to get HTML content of a web page for a given URL programatically with c# ?

This article shows how to grab the HTML content of a given url programatically with C#.DotNet Framework provides different classes to send http web request programatically which are under namespaec 'System.Net'.For the current implementation i'm using types 'HttpWebRequest' to send hhtp request and 'HttpWebResponse' to capture the response send by web server.
The code looks like this

static void Main(string[] args)
{
string html;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://google.com");
try
{
HttpWebResponse res =(HttpWebResponse)req.GetResponse();
if (res != null)
{
if (res.StatusCode == HttpStatusCode.OK)
{
Stream stream = res.GetResponseStream();
using (StreamReader reader = new StreamReader(stream))
{
html = reader.ReadToEnd();
}
Console.Write(html);
Console.Read();
}
res.Close();
}
}
catch { }
}


Both the HttpWebRequest & HttpWebResponse objects doesnt support any constructors.The Type WebRequest supports static method called Create() which takes URL of the web page as parameter & returns 'WebRequest' object which can be casted to 'HttpWebRequest'.
Similarly HttpWebRequest object supports method GetResponse() which returns WebResponse object which can be casted into HttpWebResponse.The response of the internet resource can be accessed from HttpWebResponse object by calling 'GetResponseStream()' which returns a stream.
If the method of the http request to internet source is POST & it is having any PostData which to be attached to http request can be sent by attaching it to HttpRequest Stream which can be accessed by calling the method GetRequestStream() of HttpWebRequest object.

Saturday, October 18, 2008

How to Compress & Decompress Strings in C# ?

This article show how to compress strings in C#. Dotnet Framework provides classes to compress the streams which can be found in 'System.IO.Compression'.The class i'm going to use is 'GZipStream'.Basically this class compresses streams , one can be use this class to compress even files.But here i'm using to compress string.

As i said class 'GZipStream' provides methods to compress streams, i'm converting my string to stream first then compressing the resulting stream.
Code looks like this
//
For Compression
public string Compress(string text)
{
byte[] buffer = Encoding.UTF8.GetBytes(text);
MemoryStream ms = new MemoryStream();
using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true))
{
zip.Write(buffer, 0, buffer.Length);
}
ms.Position = 0;
byte[] compressed = new byte[ms.Length];
ms.Read(compressed, 0, compressed.Length);
byte[] gzBuffer = new byte[compressed.Length + 4];
System.Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);
System.Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4);
return Convert.ToBase64String(gzBuffer);
}



For decompression
public string Decompress(string compressedText)
{
byte[] gzBuffer = Convert.FromBase64String(compressedText);
using (MemoryStream ms = new MemoryStream())
{
int msgLength = BitConverter.ToInt32(gzBuffer, 0);
ms.Write(gzBuffer, 4, gzBuffer.Length - 4);
byte[] buffer = new byte[msgLength];
ms.Position = 0;
using (GZipStream zip = new GZipStream(ms, CompressionMode.Decompress))
{
zip.Read(buffer, 0, buffer.Length);
}
return Encoding.UTF8.GetString(buffer);
}
}

Note:The compression rate will be higher for larger strings.As the number character in string increases the rate of compression will be increased.