Why can't I read Http Request Input stream twice?

Michael Levy

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);
    }
}
Patko

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why I can't reuse WebClient to make the same request twice?

From Dev

Why can't I read the input string using this scanf function?

From Dev

Why can't I use res.json() twice in one post request?

From Dev

How can I read the HTTP post request?

From Dev

Why can't make http request in android?

From Dev

Why can't I seem to read an entire compressed file from a URL stream?

From Dev

Why I can't get the user input when &0 stream is redirected

From Dev

Why can I free memory twice, but can't in different situations?

From Java

I cannot share my Observable stream properly and my http request calls twice

From Dev

Can't read Data input Stream in java IO

From Dev

Can't read Data input Stream in java IO

From Dev

Why AlarmManager executes Async Http Request twice?

From Dev

Why I can't use datepicker twice in the same table?

From Dev

Why can't I iterate twice over the same data?

From Dev

Why can't I read whole file?

From Dev

Why can't I read whole file?

From Dev

Why I can't read file?

From Dev

Why doesn't `loadFont` close input stream? Should I close it?

From Dev

Why can't I pass a string to @input

From Dev

Why can't I input it at once and output it?

From Dev

Why can't I get the input value?

From Java

How can I read a header from an http request in golang?

From Dev

Why I can't read a read only file?

From Dev

Why i can not read angular js input value with jquery?

From Dev

Why is the c++ input file stream checked twice here?

From Dev

Can't read the same InputStream twice

From Dev

Why can't I pattern match on Stream.empty in Scala?

From Dev

Java 8 Streams - Why can't I sum a Stream of Integers?

From Dev

Why can't I use filter as my last step in a stream

Related Related

HotTag

Archive