Saturday, October 25, 2008

Some points on using statement in C# !

* using Statement provides standard way of using objects which implements IDisposable interface.
*generally IDisposable objects are those wraps unmanaged resources in them & hence Dispose() method can be called to release unmanahed resource.
*If IDisposable object is declared & instantiated in using statement then Dispose() method will be automatically called while control leaving the using block.
*The IDisposable can be declared & instantiated as other objects noramally & call the Dispose() method to release unmanaged resources.
*But putting it in using statement ensures that the object is disposed even when any exception occured while calling methods on object.
*This is equivalent to intantiating IDisposable object in try/ catch block & calling Dispose() in finally block.
*With in the using block the object is readonly , it cant reassigned.

Below code shows how to use IDisposable objects

using (StreamReader streamreader = File.OpenText(@"FilePath"))


string filetext = streamreader.ReadToEnd();


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("");
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();
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.