小编bla*_*lah的帖子

帮助从HttpContext.InputStream中读取JSON

我创建了一个HttpModule来捕获审计请求.

对于Web方法的Ajax请求,我还想记录与请求关联的JSON数据.

例如请求

POST /MyPage.aspx/AddRecord HTTP/1.1
x-requested-with:XMLHttpRequest
Accept-Language:en-gb
Referer:http:// fiddlerlocal:5000/AddRecord.aspx
Accept:application/json,text/javascript,/
Content-类型:application/json; charset = utf-8
UA-CPU:x86
Accept-Encoding:gzip,deflate
User-Agent:Mozilla/4.0(兼容; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)
主机:fiddlerlocal:5000
内容长度:287
连接:Keep-Alive
Pragma:no-cache
Cookie:.....
{"id" : "282aa3b5-b55f-431C-916e-60433fdb61c0", "日期":"

我已经尝试了各种方法从HttpContext.InputStream中读取JSON({"id":"282aa3b5-b55f-431c-916e-60433fdb61c0","date":"8-6-2010"}).

例1:

StreamReader reader = new StreamReader(request.InputStream);
string encodedString = reader.ReadToEnd(); - ReadToEnd返回一个空字符串

例2:

using(MemoryStream ms = new MemoryStream())
{
byte [] buffer = new byte [request.ContentLength];
request.InputStream.Read(buffer,0,request.ContentLength);
ms.Write(buffer,0,request.ContentLength); - 字节数组包含正确的字节数但每个字节的值为0 …

asp.net json inputstream xmlhttprequest httpmodule

5
推荐指数
2
解决办法
1万
查看次数

标签 统计

asp.net ×1

httpmodule ×1

inputstream ×1

json ×1

xmlhttprequest ×1