Help reading JSON from HttpContext.InputStream(帮助从 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
基础教程推荐
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- SSE 浮点算术是否可重现? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01