Error (HttpWebRequest): Bytes to be written to the stream exceed the Content-Length bytes size specified
Asked Answered
Q

2

12

I can't seem to figure out why I keep getting the following error:

Bytes to be written to the stream exceed the Content-Length bytes size specified.

at the following line:

writeStream.Write(bytes, 0, bytes.Length);

This is on a Windows Forms project. If anyone knows what is going on here I would surely owe you one.

    private void Post()
    {


        HttpWebRequest request = null;
        Uri uri = new Uri("xxxxx");
        request = (HttpWebRequest)WebRequest.Create(uri);
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        XmlDocument doc = new XmlDocument();
        doc.Load("XMLFile1.xml");
        request.ContentLength = doc.InnerXml.Length;
        using (Stream writeStream = request.GetRequestStream())
        {
            UTF8Encoding encoding = new UTF8Encoding();
            byte[] bytes = encoding.GetBytes(doc.InnerXml);
            writeStream.Write(bytes, 0, bytes.Length);
        }
        string result = string.Empty;

        request.ProtocolVersion = System.Net.HttpVersion.Version11;
        request.KeepAlive = false;
        try
        {
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (Stream responseStream = response.GetResponseStream())
                {
                    using (System.IO.StreamReader readStream = new System.IO.StreamReader(responseStream, Encoding.UTF8))
                    {
                        result = readStream.ReadToEnd();
                    }
                }
            }
        }
        catch (Exception exp)
        {
            // MessageBox.Show(exp.Message);
        }
    }
Quennie answered 12/9, 2015 at 9:25 Comment(0)
S
18

There are three possible options

  • Fix the ContentLength as described in the answer from @rene

  • Don't set the ContentLength, the HttpWebRequest is buffering the data, and sets the ContentLength automatically

  • Set the SendChunked property to true, and don't set the ContentLength. The request is send chunk encoded to the webserver. (needs HTTP 1.1 and has to be supported by the webserver)

Code:

...
request.SendChunked = true;
using (Stream writeStream = request.GetRequestStream())
{ ... }
Servant answered 12/9, 2015 at 9:46 Comment(1)
Not setting the content length got me through this problem. Off to the next one! thank you.Quennie
P
14

The Encoded byte array from your InnerXml might be longer as some characters in an UTF8 encoding take up 2 or 3 bytes for a single character.

Change your code as follows:

    using (Stream writeStream = request.GetRequestStream())
    {
        UTF8Encoding encoding = new UTF8Encoding();
        byte[] bytes = encoding.GetBytes(doc.InnerXml);
        request.ContentLength = bytes.Length;
        writeStream.Write(bytes, 0, bytes.Length);
    }

To show exactly what is going on, try this in LINQPad:

var s = "é";
s.Length.Dump("string length");
Encoding.UTF8.GetBytes(s).Length.Dump("array length");

This will output:

 string length: 1 
 array length:  2 

and now use an e without the apostrophe:

var s = "e";
s.Length.Dump("string length");
Encoding.UTF8.GetBytes(s).Length.Dump("array length");

which will output:

string length: 1 
array length:  1 

So remember: string length and the number of bytes needed for a specific encoding might differ.

Probe answered 12/9, 2015 at 9:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.