c# – 为什么我无法读取Http请求输入流两次?
发布时间:2020-12-15 06:45:23 所属栏目:百科 来源:网络整理
导读:我正在加入一些调试代码来测试一些事情,然后调试代码的行为不如预期.下面的示例是一个简化的代码来演示我的问题. 这是在.NET 4中并使用WebApi,我试图在调试代码中打印出http请求的正文.为了做到这一点,我寻求输入流回读流.它第一次工作正常,但如果我再次读取
我正在加入一些调试代码来测试一些事情,然后调试代码的行为不如预期.下面的示例是一个简化的代码来演示我的问题.
这是在.NET 4中并使用WebApi,我试图在调试代码中打印出http请求的正文.为了做到这一点,我寻求输入流回读流.它第一次工作正常,但如果我再次读取,我会得到一个空字符串. 为什么我不能再次回头看看InputStream?在下面的示例中,body2始终为空.在第二组中,CanSeek仍然为真,第二次调用ReadToEnd()返回一个空字符串覆盖默认值. using System.IO; using System.Net; using System.Net.Http; using System.Web; using System.Web.Http; public class TestController : ApiController { public class TestOutuput { public string firstRead; public string secondRead; } public HttpResponseMessage Post() { string body1 = "default for one"; string body2 = "default for two"; if (HttpContext.Current.Request.InputStream.CanSeek) { HttpContext.Current.Request.InputStream.Seek(0,System.IO.SeekOrigin.Begin); } using (var reader = new StreamReader(HttpContext.Current.Request.InputStream)) { body1 = reader.ReadToEnd(); } if (HttpContext.Current.Request.InputStream.CanSeek) { HttpContext.Current.Request.InputStream.Seek(0,System.IO.SeekOrigin.Begin); } using (var reader2 = new StreamReader(HttpContext.Current.Request.InputStream)) { // this is always empty,even after seek back to origin body2 = reader2.ReadToEnd(); } TestOutuput testOutput = new TestOutuput() { firstRead = body1,secondRead = body2 }; HttpResponseMessage response = new HttpResponseMessage(); return Request.CreateResponse<TestOutuput>(HttpStatusCode.OK,testOutput); } } 解决方法
StreamReader在处理时调用给定流上的Dispose.要使流打开使用StreamReader的
appropriate constructor.
或者更好的是,只需将其复制到缓冲区.从MSDN:
例如见this question. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |