Is URLConnection broken ?



Hi

I am using URLConnection from an applet to post data back to the webserver and in most cases this works like a charm. But sometimes it seems like Java adds null bytes to the posted data.

My client side code is this:

URLConnection conn = .....
byte encrypted[] = .....
conn.setRequestProperty("Connection", "Close");
conn.setRequestProperty("Content-Length", "" + encrypted.length);
conn.setRequestProperty("User-Agent", String.valueOf(port));
conn.setRequestProperty("Content-Type", "application/binary; charset=" + localCharset);
conn.setRequestProperty("X-CryptoToken", cryptotoken);
OutputStream out = conn.getOutputStream();
out.write(encrypted);
out.close();

So, the content length is set to the length of the 'encrypted' byte array which is also the only piece of data send on the connections output stream.

But sometimes I get a reply like the following:

<hex>
0000000: 504f 5354 202f 2048 5454 502f 312e 310d POST / HTTP/1.1.
0000010: 0a43 6f6e 7465 6e74 2d4c 656e 6774 683a .Content-Length:
0000020: 2034 300d 0a55 7365 722d 4167 656e 743a 40..User-Agent:
0000030: 2033 3230 360d 0a43 6f6e 7465 6e74 2d54 3206..Content-T
0000040: 7970 653a 2061 7070 6c69 6361 7469 6f6e ype: application
0000050: 2f62 696e 6172 793b 2063 6861 7273 6574 /binary; charset
0000060: 3d55 5446 2d38 0d0a 582d 4372 7970 746f =UTF-8..X-Crypto
0000070: 546f 6b65 6e3a 2064 6633 6139 6231 332d Token: df3a9b13-
0000080: 6238 6530 2d34 3362 302d 6237 3666 2d33 b8e0-43b0-b76f-3
0000090: 3965 6436 3265 3130 3234 360d 0a43 6163 9ed62e10246..Cac
00000a0: 6865 2d43 6f6e 7472 6f6c 3a20 6e6f 2d63 he-Control: no-c
00000b0: 6163 6865 0d0a 5072 6167 6d61 3a20 6e6f ache..Pragma: no
00000c0: 2d63 6163 6865 0d0a 486f 7374 3a20 7777 -cache..Host: ww
00000d0: 772e 6b6f 6d6f 6776 696e 642e 646b 0d0a w.komogvind.dk..
00000e0: 4163 6365 7074 3a20 7465 7874 2f68 746d Accept: text/htm
00000f0: 6c2c 2069 6d61 6765 2f67 6966 2c20 696d l, image/gif, im
0000100: 6167 652f 6a70 6567 2c20 2a3b 2071 3d2e age/jpeg, *; q=.
0000110: 322c 202a 2f2a 3b20 713d 2e32 0d0a 436f 2, */*; q=.2..Co
0000120: 6f6b 6965 3a20 4b4f 565f 5245 4649 443d okie: KOV_REFID=
0000130: 646f 7562 6c65 2533 4173 6f6c 736b 696e double%3Asolskin
0000140: 3730 363b 2063 6f6f 6b69 6566 6972 7374 706; cookiefirst
0000150: 7669 7369 743d 313b 2063 6f6f 6b69 6562 visit=1; cookieb
0000160: 7275 6765 726e 6176 6e32 3d6a 656e 7365 rugernavn2=jense
0000170: 6e36 3634 3b20 636f 6f6b 6965 6672 6565 n664; cookiefree
0000180: 7669 703d 7965 733b 2063 6f6f 6b69 6546 vip=yes; cookieF
0000190: 6f6e 7453 697a 653d 6d65 6469 756d 3b20 ontSize=medium;
00001a0: 636f 6f6b 6965 496e 666f 5465 7874 3d66 cookieInfoText=f
00001b0: 7269 656e 6473 3b20 636f 6f6b 6965 536d riends; cookieSm
00001c0: 696c 6579 5479 7065 3d61 6e69 6d61 7465 ileyType=animate
00001d0: 643b 2063 6f6f 6b69 6554 6865 6d65 3d73 d; cookieTheme=s
00001e0: 7461 6e64 6172 643b 2063 6f6f 6b69 6546 tandard; cookieF
00001f0: 6f6e 7457 6569 6768 743d 6e6f 726d 616c ontWeight=normal
0000200: 3b20 636f 6f6b 6965 5265 7369 7a65 4368 ; cookieResizeCh
0000210: 6174 3d65 7870 616e 643b 2062 7275 6765 at=expand; bruge
0000220: 726e 6176 6e32 3d6a 656e 7365 6e36 3634 rnavn2=jensen664
0000230: 3b20 6361 6d70 6169 676e 3038 3038 5374 ; campaign0808St
0000240: 6174 7573 3d2d 313b 2063 6f6f 6b69 655f atus=-1; cookie_
0000250: 7573 6572 6e61 6d65 3d6a 656e 7365 6e36 username=jensen6
0000260: 3634 3b20 636f 6f6b 6965 5f70 6173 7377 64; cookie_passw
0000270: 6f72 643d 6862 6c75 636b 793b 2063 6f6f ord=hblucky; coo
0000280: 6b69 655f 7361 7665 696e 666f 3d6f 6e3b kie_saveinfo=on;
0000290: 2063 6f6f 6b69 6555 6e69 7175 6556 6973 cookieUniqueVis
00002a0: 6974 6f72 3d31 3b20 5048 5053 4553 5349 itor=1; PHPSESSI
00002b0: 443d 7663 6973 6c30 6b31 3167 7570 6a70 D=vcisl0k11gupjp
00002c0: 6966 6173 6365 6e6b 6236 6134 3b20 6c6f ifascenkb6a4; lo
00002d0: 6769 6e3d 313b 2075 6e64 6566 696e 6564 gin=1; undefined
00002e0: 3b20 5f5f 7574 6d61 3d31 3231 3936 3534 ; __utma=1219654
00002f0: 3537 2e37 3632 3833 3636 3239 2e31 3233 57.762836629.123
0000300: 3136 3931 3936 352e 3132 3533 3739 3935 1691965.12537995
0000310: 3334 2e31 3235 3338 3134 3832 392e 3236 34.1253814829.26
0000320: 333b 205f 5f75 746d 7a3d 3132 3139 3635 3; __utmz=121965
0000330: 3435 372e 3132 3439 3135 3334 3132 2e31 457.1249153412.1
0000340: 3732 2e33 2e75 746d 6363 6e3d 286f 7267 72.3.utmccn=(org
0000350: 616e 6963 297c 7574 6d63 7372 3d67 6f6f anic)|utmcsr=goo
0000360: 676c 657c 7574 6d63 7472 3d6b 6f6d 6f67 gle|utmctr=komog
0000370: 7669 6e64 7c75 746d 636d 643d 6f72 6761 vind|utmcmd=orga
0000380: 6e69 633b 205f 5f75 746d 633d 3132 3139 nic; __utmc=1219
0000390: 3635 3435 370d 0a58 2d56 6172 6e69 7368 65457..X-Varnish
00003a0: 3a20 3332 3839 3531 3131 3933 0d0a 582d : 3289511193..X-
00003b0: 466f 7277 6172 6465 642d 666f 723a 2036 Forwarded-for: 6
00003c0: 322e 3139 382e 3139 322e 3338 0d0a 436f 2.198.192.38..Co
00003d0: 6e6e 6563 7469 6f6e 3a20 436c 6f73 650d nnection: Close.
00003e0: 0a0d 0a00 0000 0000 0000 0000 0000 0000 ................
00003f0: 0000 0000 0000 0000 0000 0000 0000 0000 ................
0000400: 0000 0018 dba2 0b41 f6df 4403 a128 eae4 .......A..D..(..
0000410: accf 8bde 8c85 8423 e917 62b9 0316 67f6 .......#..b...g.
0000420: 6843 d261 1003 e542 7359 10 hC.a...BsY.
</hex>

Content-length is 40, but there are 72 bytes after the "\r\n\r\n". The last 40 bytes are the correct data.
But where do the 32 null bytes come from ?

I can "fix" this on the server side by reading the last 40 bytes of the posted data, but I would rather fix it correctly.

Best,
Robert
.