帮助从 HttpContext.InputStream 读取 JSON

Help reading JSON from HttpContext.InputStream(帮助从 HttpContext.InputStream 读取 JSON)

本文介绍了帮助从 HttpContext.InputStream 读取 JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have created a HttpModule to capture requests for auditing purposes.

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

For Ajax requests to a web method I would like to also log the JSON data associated with the request.

例如请求

POST/MyPage.aspx/AddRecord HTTP/1.1
x-requested-with: XMLHttpRequest
接受语言:en-gb
推荐人:http://fiddlerlocal:5000/AddRecord.aspx
接受:应用程序/json、文本/javascript、/
内容类型:应用程序/json;字符集=utf-8
UA-CPU:x86
接受编码:gzip、deflate
用户代理: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
连接:保持活动
编译指示:无缓存
饼干:.....
{"id":"282aa3b5-b55f-431c-916e-60433fdb61c0","date":"8-6-2010"}

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-Type: application/json; charset=utf-8
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; 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)
Host: fiddlerlocal:5000
Content-Length: 287
Connection: Keep-Alive
Pragma: no-cache
Cookie: .....
{"id":"282aa3b5-b55f-431c-916e-60433fdb61c0","date":"8-6-2010"}

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

I have tried a variety of methods to read the JSON ({"id":"282aa3b5-b55f-431c-916e-60433fdb61c0","date":"8-6-2010"}) from the HttpContext.InputStream.

示例 1:

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

StreamReader reader = new StreamReader(request.InputStream);
string encodedString = reader.ReadToEnd(); -- ReadToEnd returns an empty string

示例 2:

使用 (MemoryStream ms = new MemoryStream())
{
字节[] 缓冲区 = 新字节[request.ContentLength];
request.InputStream.Read(buffer, 0, request.ContentLength);
ms.Write(buffer, 0, request.ContentLength);-- 字节数组包含正确的字节数,但每个字节的值为 0 - 如何编码?
返回 Convert.ToBase64String(ms.ToArray());-- 什么都不做
返回 Encoding.UTF8.GetString(ms.ToArray());-- 什么都不做
}

using (MemoryStream ms = new MemoryStream())
{
byte[] buffer = new byte[request.ContentLength];
request.InputStream.Read(buffer, 0, request.ContentLength);
ms.Write(buffer, 0, request.ContentLength); -- The byte array contains the correct number of bytes but each byte has a value of 0 - encoded some how?
return Convert.ToBase64String(ms.ToArray()); -- doesn't do anything
return Encoding.UTF8.GetString(ms.ToArray()); -- doesn't do anything
}

如何成功地从 HttpContext.InputStream 中提取数据?

How can I successfully extract the data from HttpContext.InputStream?

提前致谢.

推荐答案

我需要在阅读之前重置流的位置...

I needed to reset the position of the stream before reading...

request.InputStream.Position = 0;
使用 (StreamReader inputStream = new StreamReader(request.InputStream))
{
返回 inputStream.ReadToEnd();
}

request.InputStream.Position = 0;
using (StreamReader inputStream = new StreamReader(request.InputStream))
{
return inputStream.ReadToEnd();
}

这篇关于帮助从 HttpContext.InputStream 读取 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:帮助从 HttpContext.InputStream 读取 JSON

基础教程推荐