为什么我无法读取HTTP请求的输入流两次? [英] Why can't I read Http Request Input stream twice?

查看:1926
本文介绍了为什么我无法读取HTTP请求的输入流两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我把一些调试code测试一些东西,然后调试code并没有像预期的那样。下面的例子是一个简化的code来证明我的问题。

这是在.NET 4中使用的WebAPI,我想打印出调试code中的HTTP请求的主体。要做到这一点,我所寻求的输入流背部和读取流。它工作正常的第一次,但如果我试着看了一遍,我得到一个空字符串。

为什么我不能求回头去读InputStream的第二次?在下面的例子中,body2总是空的。在第二组,CanSeek仍然是正确的,并ReadToEnd()的第二次调用返回一个空字符串覆盖默认。

 使用System.IO;
使用System.Net;
使用System.Net.Http;
使用的System.Web;
使用System.Web.Http;

公共类的TestController:ApiController
{

    公共类TestOutuput
    {
        公共字符串firstRead;
        公共字符串secondRead;
    }

    公众的Htt presponseMessage帖​​子()
    {
        字符串body1 =默认为一人;
        字符串body2 =默认为二;
        如果(HttpContext.Current.Request.InputStream.CanSeek)
        {
            HttpContext.Current.Request.InputStream.Seek(0,System.IO.SeekOrigin.Begin);
        }
        使用(VAR读卡器=新的StreamReader(HttpContext.Current.Request.InputStream))
        {
            body1 = reader.ReadToEnd();
        }

        如果(HttpContext.Current.Request.InputStream.CanSeek)
        {
            HttpContext.Current.Request.InputStream.Seek(0,System.IO.SeekOrigin.Begin);
        }
        使用(VAR reader2 =新的StreamReader(HttpContext.Current.Request.InputStream))
        {
            //这始终是空的,甚至在寻求回原籍
            body2 = reader2.ReadToEnd();
        }

        TestOutuput testOutput =新TestOutuput(){firstRead = body1,secondRead = body2};
        HTT presponseMessage响应=新HTT presponseMessage();
        返回Request.CreateResponse< TestOutuput>(的HTTPStatus code.OK,testOutput);
    }
}
 

解决方案

的StreamReader 要求处置在给定的流时,处置。离开流开放使用适当的构造,在的StreamReader 。 或者更好的是,只需将它复制到缓冲区中。从MSDN:

  

从流读取数据时,它是更有效地使用的缓冲区   是大小相同的数据流的内部缓冲器。

请参阅<一href="http://stackoverflow.com/questions/1678846/how-to-log-request-inputstream-with-httpmodule-then-reset-inputstream-position">this 的问题,例如。

I was putting in some debugging code to test some things, and then the debug code didn't behave as expected. The example below is a simplified code to demonstrate my question.

This is in .NET 4 and using WebApi, I'm trying to print out the body of the http request in the debug code. To do this I seek the Input stream back and read the stream. It works fine the first time, but if I try to read it again, I get an empty string.

Why can't I seek back and read the InputStream a second time? In the example below, body2 is always empty. In the second set, CanSeek is still true and the the second call to ReadToEnd() returns an empty string overwriting the default.

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 calls Dispose on given stream when disposed. To leave the stream open use the appropriate constructor for the StreamReader. Or better yet, just copy it to a buffer. From 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.

See this question for example.

这篇关于为什么我无法读取HTTP请求的输入流两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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