Reading the Html Content of an URL
Reading the Html Content of an URL
Reading the Html Content of an URL
Code
using System.Net;
using System.Text;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string contents = ReadHtmlFromUrl("http://www.google.com");
Literal1.Mode = LiteralMode.Encode;
Literal1.Text = contents;
}
public string ReadHtmlFromUrl(string Url)
{
const int TIMEOUT = 3000;
HttpWebRequest myWebRequest = null;
HttpWebResponse myWebResponse = null;
Stream receiveStream = null;
Encoding encode = null;
StreamReader readStream = null;
string content = null;
try
{
myWebRequest = HttpWebRequest.Create(Url) as HttpWebRequest;
myWebRequest.Timeout = TIMEOUT;
myWebRequest.ReadWriteTimeout = TIMEOUT;
myWebResponse = myWebRequest.GetResponse() as HttpWebResponse;
receiveStream = myWebResponse.GetResponseStream();
encode = System.Text.Encoding.GetEncoding("utf-8");
readStream = new StreamReader(receiveStream, encode);
content = readStream.ReadToEnd().ToLower();
if (readStream != null) readStream.Close();
if (receiveStream != null) receiveStream.Close();
if (myWebResponse != null) myWebResponse.Close();
}
catch (Exception ex)
{
return ex.Message;
}
finally
{
readStream = null;
receiveStream = null;
myWebResponse = null;
myWebRequest = null;
}
return content;
}
}
Code Explanation
The above code has a method ReadHtmlFromUrl, which will take an URL as parameter, return the HTML content from the WebSite. The content returned by the URL can be displayed on our page using a Literal Control. Make sure that the mode of the Literal control is set to LiteralMode.Encode.
Thanks & Regards
Paritosh Mohapatra