2012-04-30 18 views
0

我已经创建了一个c#web页面,它将侦听来自服务的POST数据。我已经看到,我可能需要使用Request.GetBufferlessInputStream(),它将读取页面数据。然后我将在我的代码中进一步处理这些数据。正确使用Request.GetBufferlessInputStream()POST数据c#

但是,我无法获取发布数据!我发现我需要使用

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

但我不确定该如何读取。我试图从0读到最后,但只获取返回的数字(假设字节数据?)。

我基本上想看到一个字符串!我一直在使用CURL来伪造一个帖子。代码在这里

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

所以我希望我的脚本输出<callback var="matt">。有任何想法吗?

回答

1

这就是你需要 -

Stream stream = Request.InputStream; 

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

string text = reader.ReadToEnd(); 
+0

完美 - 谢谢!! –

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

这读取使用非阻塞异步方法和using声明妥善处置流。