Wednesday, October 24, 2012

Getting HTML from aspx page and save to file

This post gives the sample code to download the html of an aspx and save in to a file


Add reference to System.Net. namespace.

protected void Download_Click(object sender, EventArgs e)
     {
         WebClient mydwdClient = new WebClient();
         string currentUrl = Request.Url.ToString(); // The page which needs to be downloaded
         string webpageinHTML = String.Empty;
         byte[] bytHTML;
        UTF8Encoding utfenc= new UTF8Encoding();       
         bytHTML = mydwdClient.DownloadData(currentUrl );
        webpageinHTML = utfenc.GetString(bytHTML);         
         Response.Write(webpageinHTML );                   
     }
 

Another method which i found in asp.net forum is

protected override void Render(HtmlTextWriter writer)
{
StringBuilder sbOut = new StringBuilder();
StringWriter swOut = new StringWriter(sbOut);
HtmlTextWriter htwOut = new HtmlTextWriter(swOut);
base.Render(htwOut);
string sOut = sbOut.ToString();

// Send sOut as an Email

writer.Write(sOut);
}

No comments: