How to save html page using C#

F

fmakopo

I am trying to save html page using C# because i want to save the page
on the server not on the client side.
I can do that using Javascript the problem that i am having i want to
save the page on the server.
 
A

aioe.cjb.net

I am trying to save html page using C# because i want to save the page
on the server not on the client side.
I can do that using Javascript the problem that i am having i want to
save the page on the server.

Exactly what do you want to save? Are you talking about a ASP.NET 2.0
applicaton from which you want to save the generated HTML before (or after)
sending it to the client? Or do you want to save the HTML from a random
remote web server? Or do you want to generate some HTML page and save
locally?

In either case, you'd probably want some code like this:

string htmlDocument = "<html><body>Hello world</body></html>";
FileStream fs = File.OpenWrite("C:\myfile.html");
StreamWriter writer = new StreamWriter(fs, Encoding.UTF8);
writer.Write(htmlDocument);
writer.Close();

...But of course, retrieving the actual HTML data is the challenge.

Frode Nilsen
 
G

Guest

Sample code:

private void Page_Load(object sender, System.EventArgs e)
{
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new
System.Web.UI.HtmlTextWriter(oStringWriter);
Page.RenderControl(oHtmlTextWriter);
oHtmlTextWriter.Flush();
System.IO.FileStream fs = new
System.IO.FileStream(@"C:\temp\test.htm",System.IO.FileMode.Create);
string s= oStringWriter.ToString();
byte[] b = System.Text.Encoding.UTF8.GetBytes(s);
fs.Write(b,0,b.Length);
fs.Close();
Response.End();
}

--Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top