0

i have created the website with c#.net in a page load event as

protected void Page_Load(object sender, EventArgs e)
    {

        string s = "completed.";
        byte[] bytes = System.Text.Encoding.UTF8.GetBytes(s);

        Response.OutputStream.Write(bytes, 0, bytes.Length);
    }

when i am running through browser i am getting a correct response as

Completed.

Now my question is when i run the same url through rest client i am getting the Response nody[raw] as

completed.

<!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="Default.aspx" id="form1">

<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE2MTY2ODcyMjlkZJy1PJCY5kS9nkQAfBTgrw0zeG/yMEs2VJP+7kbHC2Yp" />
</div>    <div> </div>    </form></body></html>

but i need to get only completed. as response

how is it possible ?

Waiting for your valuable comments and suggestions

GowthamanSS
  • 1,434
  • 4
  • 33
  • 58

3 Answers3

3

Try to use:

Response.Clear();
Response.OutputStream.Write(bytes, 0, bytes.Length); 
Response.End(); 
Victor
  • 653
  • 5
  • 9
  • 1
    +1. Might also need to set `buffer=true` on the page directive, and given that the output isn't HTML, also set `Response.ContentType` to `text/plain` – StuartLC Aug 30 '12 at 05:50
  • @Victor thanks for solution but i am getting error response as "Response is not available in this context". – GowthamanSS Aug 30 '12 at 06:05
  • 2
    Please try: System.Web.HttpContext context = System.Web.HttpContext.Current; System.Web.HttpResponse response = context.Response; response.Clear(); response.ContentType = "text/plain"; response.OutputStream.Write(bytes, 0, bytes.Length); response.End(); – Victor Aug 30 '12 at 06:13
1

Try clearing response.

protected void Page_Load(object sender, EventArgs e)
    {

        string s = "completed.";
        byte[] bytes = System.Text.Encoding.UTF8.GetBytes(s);

        Response.Clear();
        Response.OutputStream.Write(bytes, 0, bytes.Length);
    }

If this won't help then use method OnRender. There are some methods in page life cycle after Page_Load and i think that OutputStream is also modified there.

Michal Franc
  • 1,036
  • 10
  • 16
1

Outputting to the response stream in page_load will cause whatever you output to be put as the very first thing in the response.

That is why you are seeing "Completed" in the rest version of the reponse.

I would almost guarantee you are also getting this in the browser version but the browser is trying to shield you from it, have you tried viewing source on the html?

I have a feeling that clearing the response might not help as when the page hits its on_prerender event it will render all of the form controls / master pages etc out and it will add that gunk to the page.

Have you considered using mvc/webapi it would be a much more lightweight version to achieve what you are looking at.

Daniel Powell
  • 8,143
  • 11
  • 61
  • 108
  • Hadhi harriri has a nice small library if you went down the mvc route and wanted a bit of a library to handle the rest stuf https://github.com/hhariri/EasyHttp – Daniel Powell Aug 30 '12 at 06:09