Saturday, December 1, 2007

Get download page source of web-page into your .Net code

Here I’m downloading the page source of yahoo page and display in my page.

string strurl = "";

strurl = "http://www.yahoo.com";

StringBuilder sb=new StringBuilder();

try

{

System.Net.HttpWebRequest webreq = (System.Net.HttpWebRequest)System.Net.WebRequest.
Create(strurl);

System.Net.HttpWebResponse webres = (System.Net.HttpWebResponse)webreq.GetResponse();

StreamReader sr = new StreamReader(webres.GetResponseStream(), Encoding.Default);

while ((temp = sr.ReadLine()) != null)

{

sb.Append(temp + "\n\r");

}

string htmlcontent = sb.ToString();

Response.Write(htmlcontent);

}

catch (Exception ex)

{

Response.Write(ex.Message);

}

No comments: