using Request.GetBufferlessInputStream() correctly for POST data c#

ⅰ亾dé卋堺 提交于 2019-12-11 23:47:54

问题


I have created a c# web page which will be listening for POST data from a service. I've seen that I may need to use Request.GetBufferlessInputStream() which will read in the page data. I will then process this data further in my code.

However, I cannot get the post data! I have found that I need to use

Stream httpStream = Request.GetBufferlessInputStream();
Response.Write(httpStream.ToString());

but I am not sure how to then read that. I have tried reading from 0 to the end, but get only numbers returned (assume byte data?).

I basically want to see a string! I've been using CURL to fake a post. That code is here

curl -H "Content Type: application/xml" -d "<callback var=\"matt\">" -X POST http://localhost:1111/Default.aspx

So I would expect my script to output <callback var="matt">. Any ideas?


回答1:


This is what you need-

Stream stream = Request.InputStream;

StreamReader reader = new StreamReader(stream, Request.ContentEncoding);

string text = reader.ReadToEnd();



回答2:


using (var streamReader = new StreamReader(Request.GetBufferlessInputStream(), Request.ContentEncoding))
{
    string requestBody = await streamReader.ReadToEndAsync();
}

This reads using the non-blocking async method and the using statement properly disposes the stream.



来源:https://stackoverflow.com/questions/10386534/using-request-getbufferlessinputstream-correctly-for-post-data-c-sharp

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