Skip to content

Instantly share code, notes, and snippets.

@Dynyx
Created June 4, 2012 13:35
Show Gist options
  • Save Dynyx/2868414 to your computer and use it in GitHub Desktop.
Save Dynyx/2868414 to your computer and use it in GitHub Desktop.
Download webpage from URL
public static string DownloadWebPage(string url)
{
var webRequestObject = (HttpWebRequest)WebRequest.Create(url);
webRequestObject.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; rv:2.2) Gecko/20110201";
webRequestObject.Referer = "http://www.google.com";
try
{
var response = webRequestObject.GetResponse();
var webStream = response.GetResponseStream();
if (webStream != null)
{
var reader = new StreamReader(webStream);
var pageContent = reader.ReadToEnd();
reader.Close();
webStream.Close();
response.Close();
return pageContent;
}
return string.Empty;
}
catch (Exception)
{
return string.Empty;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment