0

I am working in an asp.net website. I need to get the current page HTML output in the Page Load event. I tried the following code. But I am not getting any output, it executes continuously.

protected void Page_Load(object sender, EventArgs e)
{
        Http(Request.Url.ToString());
}

public void Http(string url)
{
    if (url.Length > 0)
    {
        Uri myUri = new Uri(url);
        // Create a 'HttpWebRequest' object for the specified url. 
        HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(myUri);
        // Set the user agent as if we were a web browser
        myHttpWebRequest.UserAgent = @"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.4) Gecko/20060508 Firefox/1.5.0.4";

        HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
        var stream = myHttpWebResponse.GetResponseStream();
        var reader = new StreamReader(stream);
        var html = reader.ReadToEnd();
        // Release resources of response object.
        myHttpWebResponse.Close();
        Response.Write(html);
    }
}

What is wrong here?

Is there is any other way to get current page HTML output using c#?

I tried the following code also:

 protected void Page_Load(object sender, EventArgs e)
{
    Page pp = this.Page;
    StringWriter tw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(tw);
    pp.RenderControl(hw);

    string theOut = tw.ToString().Trim();
    string FilePath = @"D:\Home.txt";

    Stream s = new FileStream(FilePath, FileMode.Create);

    StreamWriter sw = new StreamWriter(s);
    sw.WriteLine(theOut);
    sw.Close(); 
}

By using the code i am able to get the HTML in the ".txt" file.But execution of this code causes "A page can have only one server-side Form tag." error. Can anybody help me to solve this?

Bisileesh
  • 1,982
  • 2
  • 19
  • 29
  • I am trying to get the current page output HTML so that i can parse it in order to get some values. – Bisileesh Jul 18 '12 at 08:48
  • So you are parsing the current page (that is to be rendered) so you can get values from it to render it again? Sounds like you should be passing those values as querystrings or with a POST to the page instead of trying to parse the html. – Karl-Johan Sjögren Jul 18 '12 at 10:07

3 Answers3

2

well, you will have to bend space-time continuum, because in Page_Load event there is no html output, and naturally your request in http method (isn't that really bad name?) will call Page_Load again.

It's a joke, you can't have html output in Page_Load event since it's not been produced yet.

Update:

You can make changes on produced output by page with HttpFilter, look at this SO answer :

https://stackoverflow.com/a/10215626/351383

Community
  • 1
  • 1
Antonio Bakula
  • 20,445
  • 6
  • 75
  • 102
  • 2
    "you will have to bend space-time continuum" can you please elaborate? – Bisileesh Jul 18 '12 at 08:54
  • 1
    You will have to get the output before it begins, so your programm needs to time travel aka bend the space-time continuum – SynerCoder Jul 18 '12 at 08:56
  • you better describe what you are trying to accomplish, you can't have html with anther request to same URI that you are rendering. – Antonio Bakula Jul 18 '12 at 09:36
  • I have an Iframe in the page,i need to get the scr of the Iframe.But i cant access it from the code behind,as the Iframe is embedded to the page by means of a Custom widget in Ektron.(I can get it by using java script-but i need to get it on server side). – Bisileesh Jul 18 '12 at 09:46
1

Page_Render event is responsible for generating HTML for the page and Unload event gets called after this. In this event you should be able to get HTML output of the page.

Nps
  • 1,638
  • 4
  • 20
  • 40
  • I tried Page_Render event.But the control doesn't go to this event after Page_Load..! When i tried Page_PreRender,the control goes to this event after Page_Load,but result is the same,it continuously executes..!! – Bisileesh Jul 18 '12 at 09:38
0

You can try this...

public override void Render(HtmlTextWriter writer):
    {
        StringBuilder renderedOutput = new StringBuilder();
        Streamwriter  strWriter = new StringWriter(renderedOutput);
        HtmlTextWriter tWriter = new HtmlTextWriter(strWriter);
        base.Render(tWriter);

        string html = tWriter.InnerWriter.ToString();

        string filename = Server.MapPath(".") + "\\data.txt";
        outputStream = new FileStream(filename, FileMode.Create);
        StreamWriter sWriter = new StreamWriter(outputStream);
        sWriter.Write(renderedOutput.ToString());
        sWriter.Flush();

        //render for output
        writer.Write(renderedOutput.ToString());
    }
flop_coder
  • 11
  • 1