Solved

Uploading from Java applet using HTTP Post method

Posted on 1997-05-09
5
235 Views
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
transfer:

      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?
0
Comment
Question by:Zero050997
5 Comments
 
LVL 3

Expert Comment

by:pc012197
ID: 1220300
Are you using a proxy server? Some proxies limit the amount of data allowed in a POST operation.

0
 
LVL 6

Expert Comment

by:jpk041897
ID: 1220301
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:

http://www.inf.uni-hohenheim.de/top/java/tutorial/networking/urls/readingWriting.html

for an example on how to do this.
0
 

Author Comment

by:Zero050997
ID: 1220302
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");


0
 
LVL 6

Expert Comment

by:jpk041897
ID: 1220303
Great,

Now contact the customer support forum of experts exchange and cancel your question so that you can get your points back.
0
 

Accepted Solution

by:
keegi earned 50 total points
ID: 1220304
Anyway, I'm greedy about points ;-) and I propose an more elegant answer (imho), using URLs instead of Socket

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

Now you can get and write to the output stream:

DataOutputStream os = new DataOutputStream(c.getOutputStream());
os.writeBytes("var1=value%2C+another&var2=third+value");
os.close();

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now