c# – 为什么我无法读取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:

When reading from a Stream,it is more efficient to use a buffer that
is the same size as the internal buffer of the stream.

例如见this question.

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...