We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


Uploading from Java applet using HTTP Post method

Zero050997 asked
Medium Priority
Last Modified: 2008-03-06

I've run into some problems uploading some ascii and binary data from a
Java applet to a cgi-script running on Apache 1.0.5 HTTP server under
FreeBSD using the POST method.

Apparently the server just can't get the content length out of the stream
I send. On a MS IIS 2 server it worked to some extent, though when the
length crossed some certain limit, the IIS recieved just the first 900
bytes (I suspect thats some kind of default for cases it can't read
content length)...

Just in case i paste here the piece of code i use to initiate data

      Socket s = new Socket("hostname", 80);

      DataOutputStream out = new DataOutputStream(s.getOutputStream());
      out.writeBytes("POST /cgi-bin/script.pl\n");
      out.writeBytes("Content-length: " + length + "\n");
      out.writeBytes("Content-type: application/octet-stream\n\n");

Does anyone have any sample code or reference URL's to check?
Watch Question

Are you using a proxy server? Some proxies limit the amount of data allowed in a POST operation.

Two other alternatives I can think of:

Try sending a \r\n pair rather than just a \n.

Try using the URLConnection class to do the post. See:


for an example on how to do this.


The problem has been solved. The header had to be sent like this:

out.writeBytes("POST /cgi-bin/skript.pl HTTP/1.0\r\n");


Now contact the customer support forum of experts exchange and cancel your question so that you can get your points back.
Anyway, I'm greedy about points ;-) and I propose an more elegant answer (imho), using URLs instead of Socket

URL gather = new URL("http://myhost/cgi-bin/gather.cgi");
URLConnection c = gather.openConnection();

Now you can get and write to the output stream:

DataOutputStream os = new DataOutputStream(c.getOutputStream());

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.