从帮助阅读HttpContext.InputStream JSON [英] Help reading JSON from HttpContext.InputStream

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

问题描述

我创建了一个的HttpModule捕捉审计目的的要求。

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

有关Ajax请求到一个Web方法,我想也记录与请求相关的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要求,具有:XMLHtt prequest结果
  接受语言:EN-GB结果
  引用者:的http:// fiddlerlocal:5000 / AddRecord.aspx 结果
  接受:应用/ JSON,文本/ javascript中,的 / 的结果
  内容类型:应用程序/ JSON的;字符集= utf-8的结果
  UA-CPU:86结果
  接受编码:gzip,紧缩结果
  用户代理: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结果
  连接:保持活动结果
  编译:no-cache的结果
  饼干:.....结果
   {ID:282aa3b5-b55f-431C-916e-60433fdb61c0,日期:2010年8月6日}

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"}

我已经尝试了各种方法来读取JSON从HttpContext.InputStream({ID:2010年8月6日:282aa3b5-b55f-431C-916e-60433fdb61c0,日期})

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的读者=新的StreamReader(request.InputStream);结果
  字符串连接codedString = reader.ReadToEnd(); - ReadToEnd的返回一个空字符串

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

例2:

使用(MemoryStream的毫秒=新的MemoryStream())结果
  {结果
     字节[]缓冲区=新的字节[request.ContentLength];结果
     request.InputStream.Read(缓冲液,0,request.ContentLength);结果,
     ms.Write(缓冲液,0,request.ContentLength); - 字节数组包含正确的字节数,但每个字节都有一个0值 - 恩codeD一些如何? 的结果
     返回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 =新的StreamReader(request.InputStream))结果
  {结果
  返回inputStream.ReadToEnd();结果
  }

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

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

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