Tuesday, January 5, 2010

Simple method to download Web resource

Had to look this up - again - so writing a quick note for future reference.

public static string Download(string url) 
{ 
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url); 
HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); 
Stream stream = httpWebResponse.GetResponseStream(); 
StreamReader streamReader = new StreamReader(stream, Encoding.ASCII); 
return streamReader.ReadToEnd(); 
}