Search code examples
c#http.net-3.5.net-2.0

Reading "chunked" response with HttpWebResponse


I'm having trouble reading a "chunked" response when using a StreamReader to read the stream returned by GetResponseStream() of a HttpWebResponse:

// response is an HttpWebResponse
StreamReader reader = new StreamReader(response.GetResponseStream());
string output = reader.ReadToEnd(); // throws exception...

When the reader.ReadToEnd() method is called I'm getting the following System.IO.IOException: Unable to read data from the transport connection: The connection was closed.

The above code works just fine when server returns a "non-chunked" response.

The only way I've been able to get it to work is to use HTTP/1.0 for the initial request (instead of HTTP/1.1, the default) but this seems like a lame work-around.

Any ideas?


@Chuck

Your solution works pretty good. It still throws the same IOExeception on the last Read(). But after inspecting the contents of the StringBuilder it looks like all the data has been received. So perhaps I just need to wrap the Read() in a try-catch and swallow the "error".


Solution

  • Haven't tried it this with a "chunked" response but would something like this work?

    StringBuilder sb = new StringBuilder();
    Byte[] buf = new byte[8192];
    Stream resStream = response.GetResponseStream();
    string tmpString = null;
    int count = 0;
    do
    {
         count = resStream.Read(buf, 0, buf.Length);
         if(count != 0)
         {
              tmpString = Encoding.ASCII.GetString(buf, 0, count);
              sb.Append(tmpString);
         }
    }while (count > 0);