Tech Off Thread

6 posts

using Response.Flush() to send specific data to client

Back to Forum: Tech Off
  • User profile image
    lesmemphis

    Hi all!

    I need to send an accepted response to the client. However, I need to send only the specific data and not all the extra html page data.

    Currently my code behind looks like this:

    Response.Write("<CallBackResponse>Accepted</CallBackResponse>");
    Response.Flush();

    This however sends a response that looks like this: (I only want the portion in bold to be sent.)

    <CallBackResponse>Accepted</CallBackResponse>    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">    <html xmlns="http://www.w3.org/1999/xhtml">  <head><title>   Payment Approved  </title><link href="style.css" rel="stylesheet" type="text/css" />      <style type="text/css">          #makePayment          {              width: 247px;          }          body           {           font-family : Calibri;          }          </style>        </head>  <body>      <form name="form1" method="post" action="BookSave.aspx" id="form1" style="width:100%; height:100%; text-align:center;">  <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTEzOTM0ODg4MzMPZBYCAgMPZBYCAgEPZBYCAgUPZBYEAgMPDxYCHgRUZXh0BRM5NDIzMTIxMjEzMTU0NjE4OTM3ZGQCCw9kFgxmD2QWAgIBD2QWAgIDDw8WAh8ABQczNjAwLjAwZGQCAQ9kFgICAQ9kFgICAQ8PFgIfAAUEVmlzYWRkAgIPZBYCAgEPZBYCAgEPDxYCHwAFD0RlcG9zaXQgUGF5bWVudGRkAgMPZBYCAgEPZBYCAgEPDxYCHwAFCk5SIHZhbiBXeWt

     

    Any ideas on how to accomplish this?

    Thanks in advance! 

  • User profile image
    evildictait​or

    What happens if you put Response.End() instead of Response.Flush()?

  • User profile image
    lesmemphis

    Hi, same data gets sent (flushed), but .End() will end the entire thread. So no code will be executed after I called .End()

  • User profile image
    cheong

    Try set HttpResponse.SuppressContent to true, then write your own content there.

    Note that this property only has effect before Response.Flush() or any of the Page/Control.Render() method are called, so call it as early as possible, ideally at Page_Load().

    Recent Achievement unlocked: Code Avenger Tier 4/6: You see dead program. A lot!
    Last modified
  • User profile image
    evildictait​or

    , lesmemphis wrote

    Hi, same data gets sent (flushed), but .End() will end the entire thread. So no code will be executed after I called .End()

    Surely that's what you want? Once you have no more data to send to the client (i.e. after your final <CallBackResponse>Accepted</CallBackResponse>) you should call Response.End() which will immediately send the data you've written to Response and then get ready for the next incoming request.

  • User profile image
    Sven Groot

    @lesmemphis: If you want to send non-HTML data, surely it's better to use a generic handler (.ashx) rather than a full web form (.aspx)? It would literally just be the following:

    <%@ WebHandler Language="C#" %>
    using System.Web;
    
    public class YourHandler : IHttpHandler 
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/xml";
            context.Response.Write("<CallbackResponse>Accepted</CallbackResponse>");
        }
    
        public bool IsReusable 
        {
            get { return false; }
        }
    }
    

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.