14

I'm setting the length of the content in my HttpURLConnection, for a PUT.

urlConnection.setRequestProperty("Content-Length", "" + responseJSONArray.toString(2).getBytes("UTF8").length);

The actual number of bytes is 74. However, when I query the content length of urlConnection I'm returned -1. Why is that? And why are lengths not equal (given that I set this)?

I must set the content-length because I'm receiving a 411 response from the server.

(Also, in the Sun examples I've seen the second argument of setRequestProperty is of type int and not String, which seems odd.)

Ken
  • 30,811
  • 34
  • 116
  • 155
  • I answered this question and I realise now that it's got an android tag. I have no idea how Google implemented `HttpURLConnection`. – Buhake Sindi Aug 15 '11 at 08:41
  • @The Elite Gentleman they would have had to agree with the existing Javadoc specification, and your misunderstandings didn't have anything to do with possible variations among implementations. – user207421 Aug 22 '11 at 10:22

3 Answers3

26

You shouldn't set this header yourself. Use setFixedLengthStreamingMode() or setChunkedTransferMode().

user207421
  • 305,947
  • 44
  • 307
  • 483
  • 1
    Thanks. I still don't understand something: I'm now doing `urlConnection.setFixedLengthStreamingMode(responseJSONArray.toString(2).getBytes("UTF8").length);`. The body-length is `74` bytes, however when I call `Log.v("app", "" + urlConnection.getContentLength());` I see `-1`. Why is that? – Ken Aug 15 '11 at 07:14
  • 1
    @SK9 that's the length of the *response.* – user207421 Aug 15 '11 at 08:20
  • Thanks. That seems needlessly ambiguous to me! – Ken Aug 15 '11 at 08:27
  • 1
    @SK9 nothing ambiguous about it. There is no reference in the Javadoc to the content length of the request. It is all about the response. – user207421 Aug 16 '11 at 00:16
  • @SK9 of course, but you asked about `getContentLength(),` which is what I answered about. – user207421 Aug 18 '11 at 11:33
  • I have set setFixedLengthStreamingMode(), but i getContentLength() still return -1 at server client. Is there someone solve this problem? – boiledwater Apr 12 '12 at 10:07
  • @boiledwater Did you set it before you started to write the output? – user207421 Apr 13 '12 at 04:24
  • @boiledwater Why? It says exactly what I have told you here. – user207421 Apr 19 '12 at 03:31
3

Also do not forget to add a setDoOutput to tell your connection you are going to send data.

Nicolas Modrzyk
  • 13,961
  • 2
  • 36
  • 40
  • Thanks. I already am (with `urlConnection.setDoOutput(true);`). – Ken Aug 15 '11 at 07:15
  • It should rightfully be -1, since you are sending data not getting some from the server. getContentLenght(): "Returns the content length in bytes specified by the response header field content-length or -1 if this field is not set." From [Android Javadoc for HttpURLConnection](http://developer.android.com/reference/java/net/HttpURLConnection.html) – Nicolas Modrzyk Aug 15 '11 at 07:39
  • 1
    Hmm - I thought the content-length field specified the byte-length of the HTTP request I was preparing. – Ken Aug 15 '11 at 07:44
  • 1
    What should be -1? The question is about the request content-length. Your quotation is about the response content-length. Your remark doesn't make sense. – user207421 Oct 21 '16 at 23:40
-2

I get same error with "Content-Length" -

        URL url = new URL(targetURL);
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "text/plain");
        connection.setRequestProperty("Authorization", authKey);
        connection.setRequestProperty("Content-Length", Integer.toString(requestJSON.getBytes().length));

And I finally deduced that it happens because one of the Json object fields contains diacritical characters.

        someCategory = "Ţepuşă";
        try {
        JSONObject postData = new JSONObject();
        postData.put("category", someCategory);
        .....

That's what the error looks like:

08-27 18:57:07.842 24263-24263/ro.nexuserp.documentecontabile E/Eror: content-length promised 491001 bytes, but received 491000
Adrian S.
  • 129
  • 1
  • 2
  • 12
  • The error is that you shouldn't set it at all. Java will set it for you. This was stated, several times, eight years before you posted. – user207421 Sep 23 '19 at 11:36