在ASP.NET MVC中获取原始请求

时间:2022-12-04 18:50:20

I need to get the raw request string. Below is an example of the http request sent to the Controller. Actually, I need Post data (last line). How can I get that?

我需要获取原始请求字符串。以下是发送到Controller的http请求的示例。实际上,我需要Post数据(最后一行)。我怎么能得到它?

Notice that I don't want to use the automatic JSON model binder. Actually, I need the raw JSON text

请注意,我不想使用自动JSON模型绑定器。实际上,我需要原始的JSON文本

POST http://www.anUrl.com/CustomExport/Unscheduled HTTP/1.1
Accept: application/json, text/javascript, */*; q=0.01
Content-Type: application/json; charset=utf-8
X-Requested-With: XMLHttpRequest
Referer: http://www.anUrl.com/CustomExport
Accept-Language: en-us
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
Host: localhost:8000
Content-Length: 102
Connection: Keep-Alive
Pragma: no-cache

{"runId":"1","fileDate":"8/20/2012","orderStartMinDate":"10/02/2012","orderStartMaxDate":"10/02/2012"}

This last line is what I need. This doesn't come in

最后一行是我需要的。这不是进来的

var input = new StreamReader(Request.InputStream).ReadToEnd();

1 个解决方案

#1


21  

At that point the stream has already been read to the end. You need to set the position of the InputStream back to the beginning before you can read it yourself.

此时,流已经被读取到最后。您需要先将InputStream的位置设置为开头,然后才能自行阅读。

Request.InputStream.Position = 0;
var input = new StreamReader(Request.InputStream).ReadToEnd();

#1


21  

At that point the stream has already been read to the end. You need to set the position of the InputStream back to the beginning before you can read it yourself.

此时,流已经被读取到最后。您需要先将InputStream的位置设置为开头,然后才能自行阅读。

Request.InputStream.Position = 0;
var input = new StreamReader(Request.InputStream).ReadToEnd();