This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

httpURLConnection content-length always 0  RSS feed

Jeff Gaer
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know I am missing something obvious, but creating a connection with the following code on java 1.4.2 the content length is always 0 even though it is explicitly set.

URL url = new URL("http://localhost:" + port);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
String testData="this is a test!";
String LEN_HDR="Content-Length";
String TYPE_HDR="Content-Type";


OutputStream os = conn.getOutputStream();
PrintStream ps = new;

here are the headers are received on the server

Content-Type: text/html
Content-Length: 0
TESTContent-Length: 15
User-Agent: Java/1.4.2
Host: localhost:91
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive

Thanks in advance for any help

Jacob Anawalt
Ranch Hand
Posts: 64
Linux Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was experiencing the same Content-Length: 0 issue with a different Java versions and for seemingly different reasons, or Jeff's code example is not complete.

I tried bumbling through setting up some code to POST to a server using URL and HttpURLConnection and every time the server would not only show Content-Length 0, but the data I was trying to write was also missing as proved by wireshark captures so the Content-Length value of 0 was correct.

Searching around I first got worried that I was experiencing bug id 6997628, "HttpURLConnection strips Content-Length header on Post". I tried different Java 1.6 versions and different JRE's on different OS's. Then I found the tutorial for reading from and writing to an HttpURLConnection [0] and that worked. It didn't make sense. My code said I was writing, but as far as I could tell I was not.


Finally I found the difference between the example code and mine. I will re-write the example code with my "mistake" in it:

Moving the write statements to after the getInputStream call seems to ignore them, probably because getInputStream calls connect() and sends the request so that it can have something to read. I don't know how I was suppose to figure this out other than .

I tried reproducing Jeff Gaer's issue, but his code snippet didn't send anything, not even the URL, until I added the getInputStream call (connect() alone didn't do the trick.) Once I did, it would set the correct Content-Length. It continued to work even after commenting out all the setRequestProperty calls and the setRequstMethod call. (Note that those calls are missing from the example.) I was expecting to see an issue with the claimed content-length not matching the written amount ("test" vs "this is a test!" and the extra linefeed) but it worked anyway. I didn't try this on Java 1.4, nor do I care to dig that up to try.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!