getting response as a string c#.net

社会主义新天地 提交于 2020-01-16 19:04:45

问题


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


回答1:


Try to use:

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



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/12190524/getting-response-as-a-string-c-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!