Amit Shadadi
Amit Shadadi

Reputation: 637

issue with Response.OutputStream.Write

I have a problem with this method, for some reason, I send request to my asp page, then read the Response stream and I get the content of my asp page along with the data I wrote into Response.OutputStream.Write(info, 0, info.Length).

In my case, I write the string "1" as bytes, and the output on the client program is:

1<!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>

</title></head>
<body>
    <form method="post" action="LauncherLogin.aspx" id="form1">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE2MTY2ODcyMjlkZMraZTexYtyyQ9NaNN0YMvYIep5peaSEIrDBqxTff6rW" />

    <div>

    </div>
    </form>
</body>
</html>

I don't want this HTML in the response I receive, how do I get rid of it?

Client Code (c# program):

public static string SendInformation(string values)
{
    ASCIIEncoding encoding = new ASCIIEncoding();

    string postData = values;
    byte[] data = encoding.GetBytes(postData);

    // Prepare web request...
    HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(serverAddress + commmunicationForm);
    myRequest.Method = "POST";
    myRequest.ContentType = "text/html";
    myRequest.ContentLength = data.Length;
    string result;

    using (Stream stream = myRequest.GetRequestStream())
    {
        stream.Write(data, 0, data.Length);
    }
    using (WebResponse response = myRequest.GetResponse())
    {
        using (var reader = new StreamReader(response.GetResponseStream()))
        {
            result = reader.ReadToEnd();
            return result;
        }
    }
}

ASP.aspx.cs Code (Server Side):

if (dt.Rows.Count > 0)
{
    Response.Clear();
    Response.ClearContent();
    Response.ClearHeaders();
    Response.Buffer = false;
    string errNum = dt.Rows[0][0].ToString();
    byte[] info = Encoding.ASCII.GetBytes(errNum);
    Response.OutputStream.Write(info, 0, info.Length);
    Response.Flush();
}

Even if I do just this:

string errNum = dt.Rows[0][0].ToString();
byte[] info = Encoding.ASCII.GetBytes(errNum);
Response.OutputStream.Write(info, 0, info.Length);
Response.Flush();

I still get the html code :|

I did try Response.Clear() and other .Clear() methods with no success.

Upvotes: 5

Views: 16141

Answers (2)

Karl Anderson
Karl Anderson

Reputation: 34846

Your issue is that you are trying to change the output too early in the page life cycle (you are trying to affect the total output at the event handling stage). You need to have your logic for altering content in the Render method. You can override the Render method, like this:

protected override void Render(HtmlTextWriter writer)
{
    // Your logic here
}

Read ASP.NET Page Life Cycle Overview for more information on the page events and the order in which they occur.

Upvotes: 5

cristi71000
cristi71000

Reputation: 1124

You could try Response.Flush(). This should send what you wrote and get rid of what is sent afterwards.

Upvotes: 0

Related Questions