Search code examples
c#dotnet-httpclienthttpwebresponse

Reading full response from HttpWebResponse


I need to get a full response from an API but I always getting 65536 bytes the content length shows me 176579. I tried to read continuously but still getting 65536 bytes. with postman I'm getting the full answer so what is the correct way to read from WebResponse? Here's my code:

string json = Request.ToJson(data);
string method = "POST";
string ContentType = "application/json; charset=utf-8";

HttpWebRequest client = (HttpWebRequest)WebRequest.Create(uri);
client.Method = method;
client.KeepAlive = true;
client.Timeout = 3000;
client.ReadWriteTimeout = 50000;
client.AllowWriteStreamBuffering = true;
client.ContentType = ContentType;                

using (var streamWriter = new StreamWriter(client.GetRequestStream()))
{
    streamWriter.Write(json);
    streamWriter.Flush();
    streamWriter.Close();
}

var errorResponse = client.GetResponse();
byte[] dt = ReadFully(errorResponse.GetResponseStream(), errorResponse.ContentLength);
var str = System.Text.Encoding.Default.GetString(dt);

here's the ReadFully method (copied from another answer):

private byte[] ReadFully(Stream stream, long initialLength)
{
    // If we've been passed an unhelpful initial length, just
    // use 32K.
    byte[] bytes;
    using (MemoryStream buffer = new MemoryStream((int)initialLength))
    {
        byte[] chunk = new byte[initialLength];
        int bytesRead;
        while ((bytesRead = stream.Read(chunk, 0, chunk.Length)) > 0)
        {
            buffer.Write(chunk, 0, bytesRead);
        }

        bytes = buffer.ToArray();
    }

    return bytes;
}

Solution

  • As hinted in the comments it seems that the errorResponse variable is taken from a WebException.Response. There's a limitation of 65536 bytes when reading the underlying stream. You can increase this limitation by setting the following static property:

    HttpWebRequest.DefaultMaximumErrorResponseLength = some reasonably large number