• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

httpURLConnection content-length always 0

Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 java.io.PrintStream(os);

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

Ranch Hand
Posts: 64
Netbeans IDE Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

[0] http://download.oracle.com/javase/tutorial/networking/urls/readingWriting.html

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.
    Bookmark Topic Watch Topic
  • New Topic