问题
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