Skip to content

Instantly share code, notes, and snippets.

@CharlTruter
Created March 19, 2012 12:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CharlTruter/2109322 to your computer and use it in GitHub Desktop.
Save CharlTruter/2109322 to your computer and use it in GitHub Desktop.
Loading a site's content in C# using backend code
string GetSiteContent(string url)
{
// Create the web request for the url passed to the method
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
// For the sake of this example, we'll be using the GET method.
// For other options, please see http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.method.aspx
myRequest.Method = "GET";
// Get the response and store it in a WebResponse object
using (WebResponse myResponse = myRequest.GetResponse())
{
// Read the result into a string
using (StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8))
{
string result = sr.ReadToEnd();
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment