Get remote page contents (ASPX C#)
A simple function for getting the contents of another page in your code behind
private string GetHTML(string url)
{
WebRequest request = WebRequest.Create(url);
// use logged in user credentials
request.Credentials = CredentialCache.DefaultCredentials;
try
{
// get the response
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// set the content length
int contentLength = (int)response.ContentLength;
// get the stream
Stream responseStream = response.GetResponseStream();
// Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader readStream = new StreamReader(responseStream, Response.ContentEncoding);
// create a char array
char[] data = new char[contentLength];
// load from the stream into the char array
readStream.Read(data, 0, contentLength);
// return the data as a string
return new String(data);
}
catch(Exception ex)
{
return string.Empty;
}
return string.Empty;
}
Comments