如何在ASP.NET中获取原始请求主体? [英] How to get raw request body in ASP.NET?

查看:253
本文介绍了如何在ASP.NET中获取原始请求主体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HttpApplication.BeginRequest 事件中,如何读取整个原始请求正文?当我尝试读取它时, InputStream 的长度为0,这让我相信它可能已经被ASP.NET读过了。

In the HttpApplication.BeginRequest event, how can I read the entire raw request body? When I try to read it the InputStream is of 0 length, leading me to believe it was probably already read by ASP.NET.

我试过像这样读取InputStream:

I've tried to read the InputStream like this:

using (StreamReader reader = new StreamReader(context.Request.InputStream))
{
    string text = reader.ReadToEnd();
}

但我得到的只是一个空字符串。我已经将位置重置为0,但当然一旦读取了它就会消失,所以这样做不起作用。最后,检查流的长度返回0.

But all I get is an empty string. I've reset the position back to 0, but of course once the stream is read it's gone for good, so that didn't work. And finally, checking the length of the stream returns 0.

编辑:这是针对POST请求。

This is for POST requests.

推荐答案

以下是我最终要做的事情:

Here's what I ended up doing:

//Save the request content. (Unfortunately it can't be written to a stream directly.)
context.Request.SaveAs(filePath, false);

这篇关于如何在ASP.NET中获取原始请求主体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆