Thursday, April 3, 2008

How to get web content from the web .NET

Sometimes I need to download the html content from the internet, therefore this method is very useful.


public static void GetWebContent(string url, string saveToPath)
{
if (IsValidUrl(url))
{
HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse httpRes = (HttpWebResponse)httpReq.GetResponse();
Stream fs = httpRes.GetResponseStream();
FileStream fss = new FileStream(saveToPath, FileMode.Create);
const int size = 4096;
byte[] bytes = new byte[4096];
int numBytes;
while ((numBytes = fs.Read(bytes, 0, size)) > 0)
fss.Write(bytes, 0, numBytes);
fss.Close();
}
}

No comments: