locked
how get output text from Response.OutputStream in current aspx page? RRS feed

  • Question

  • User1127173010 posted

    I want get output from current aspx Page for replace, save to file and other.

    but Error!


    Stream was not readable.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.ArgumentException: Stream was not readable.

    using (StreamReader sr = new StreamReader(Response.OutputStream))


    aspx page is empty

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="system.Test" %>
            protected void Page_Load(object sender, EventArgs e)
            {
                Response.Write("hi Fred.");
    
                string html = "";
    
                using (StreamReader sr = new StreamReader(Response.OutputStream))
                {
                    html = sr.ReadToEnd();
                }
    
                html = html.Replace("Fred", "Sara");
    
                Response.Write(html);
            }

    I want this result :

    hi Fred.hi Sara.

    how to set Stream to readable.

    Saturday, February 10, 2018 10:13 PM

All replies

  • User475983607 posted

    Replace the string before committing the string to the response.

    Can you explain the problem you are trying to solve?
    Saturday, February 10, 2018 10:16 PM
  • User1127173010 posted


    Replace the string before committing the string to the response.

    I know!


    Can you explain the problem you are trying to solve?

    problem is VERY BIG.

    I use IHttpHandler

    so i do not use execute this page from another page, bacause occure LOOP

    for load page, i use this Code :

    Type type = BuildManager.GetCompiledType(AbsolutePath);
    Page page = (Page)Activator.CreateInstance(type);
    page.ProcessRequest(HttpContext.Current);

    i want replace value from page!

    i can use WebRequest

    WebRequest request = WebRequest.Create(MyInnerPagePath);

    but WebRequest used for foreign page and if use WebRequest in inner page, every requests It runs twice.

    It is strange, why not exist solution? why Response.OutputStream.CanRead is read only?

    Saturday, February 10, 2018 10:46 PM
  • User475983607 posted

    I use IHttpHandler

    so i do not use execute this page from another page, bacause occure LOOP

    Where is this loop?  Can you provide code that reproduces the issue or explain clearly so we can understand what you are trying to do?

    i want replace value from page!

    Generally this is done using server controls and markup, the main tools of ASP Web Forms.  If you need to change content in the pipeline then use an HttpHandler. 

    but WebRequest used for foreign page and if use WebRequest in inner page, every requests It runs twice.

    You'll need to clarify this statement.

    It is strange, why not exist solution? why Response.OutputStream.CanRead is read only?

    It is strange that you are trying to change content in the Page_Load event.  Why can't you simple return the HTML as you like in the first place?  Which, by the way, is the main purpose of ASP, to create dynamic HTML.  Why are you adding content then changing the content in the Page_Load?

    Can you explain the problem you are trying to solve?

    Saturday, February 10, 2018 11:45 PM
  • User1596461147 posted

    Hello mgebhard,

    I have a same problem.
    We have some InfoPath form with Code Behind.
    I've got some bussines task, that when user types some VAT ID, there is required to check if the VAT ID is valid or not and return info as message box.
    OK, I've used some Web Service Call to VIES

    string dicValue = FormMethods.GetIpFieldValue(currentForm, formFields.PoDicIPField);
    string countryCode = dicValue.Substring(0, 2);
    string vatId = dicValue.Substring(2, (dicValue.Length - 2));
    
    this.DataConnections["WebServices-Query-SOAP-CheckVat"].Execute();
    bool isVatValid = FormMethods.CheckVAT(this.DataSources);
    string notification = "Toto DIČ je platné.";
    if (isVatValid != true)
    {
        notification = "Toto DIČ je neplatné!";
    }

    Then I've tried to send an alert back to page.

    HttpContext currentContext = System.Web.HttpContext.Current;
    currentContext.Response.Write("<script type=\"text/javascript\">alert('" + notification + "'); </script>");

    And my problem starts after the user cliks "OK" at alert (loosing page content).
    Because when InfoPath form loads, it loads context of "FormServer.aspx" page.
    When User Clicks on button InfoPath form loads, it loads context of "PostBack.FormServer.aspx" page.
    So I need to:
    1) Store original response OutputStream from load
    2) Send an Alert
    3) Rewrite currentContext.Response by original response from load

    Could you help me please with this task?
    Tanks

    Saturday, August 18, 2018 1:52 PM
  • User475983607 posted

    1) Store original response OutputStream from load
    2) Send an Alert
    3) Rewrite currentContext.Response by original response from load

    The previous thread is complaining about changing the response stream but the response stream, by default, is read only.  Generally this is accomplished in the HTTP pipeline not an ASPX page. 

    Your question is more of an issue understanding web application fundamentals and HTTP.  There is one request and one response stream in the HTTP protocol.  By the time the user sees the alert, the response stream in already loaded in the browser and no longer exists on the server.  If you need the user to interact with a remote process you'll need two requests and responses and you'll need to write code that maintains state on the server. 

    Also this is an ASP.NET support forum.  I recommend visiting an InfoPath support forum as you'll receive better support.

    Saturday, August 18, 2018 2:12 PM