aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes httpURLConnection content-length always 0 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "httpURLConnection content-length always 0" Watch "httpURLConnection content-length always 0" New topic
Author

httpURLConnection content-length always 0

Jeff Gaer
Ranch Hand

Joined: Jun 04, 2001
Posts: 99
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";

conn.setRequestProperty(TYPE_HDR,"text/html");
conn.setRequestProperty(LEN_HDR,String.valueOf(testData.length()));
conn.setRequestProperty("TEST"+LEN_HDR,String.valueOf(testData.length()));
conn.setDoOutput(true);
conn.setRequestMethod("POST");

OutputStream os = conn.getOutputStream();
PrintStream ps = new java.io.PrintStream(os);
ps.println("test");
ps.flush();

here are the headers are received on the server


POST / HTTP/1.1
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

Jeff


Sun Certified Java Programmer Java 2<P>Jeff Gaer
Jacob Anawalt
Ranch Hand

Joined: Oct 27, 2010
Posts: 32

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.

Jacob
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: httpURLConnection content-length always 0