Ploblem doing HTTP POST via URLConnection

M

Mário

Hello,

I am trying to post an object to a servlet via HTTP using the
following code,
but unfortunately, nothing is sent to the server:

private static void doJavaPost() throws IOException {
URL url = new URL("http://localhost:9999/myservlet");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
ByteArrayOutputStream data = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(data);
out.writeObject(new Date());
out.close();
OutputStream os = connection.getOutputStream();
data.writeTo(os);
os.flush();
os.close();
}

Does someone know why this does not generate a POST request?

Regards,
Mário
 
L

Lothar Kimmeringer

Mário said:
I am trying to post an object to a servlet via HTTP using the
following code,
but unfortunately, nothing is sent to the server:

private static void doJavaPost() throws IOException {
URL url = new URL("http://localhost:9999/myservlet");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
ByteArrayOutputStream data = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(data);
out.writeObject(new Date());
out.close();
OutputStream os = connection.getOutputStream();
data.writeTo(os);
os.flush();
os.close();
}

Does someone know why this does not generate a POST request?

Because you don't ask the connection to do that. Call
connection.getInputStream() and the request will be sent.
BTW: You can skip the writing to the ByteArrayOutputStream
and initialized the ObjectOutputStream with
connection.getOutputStream()
Calling of os.close() isn't needed, either.


Regards, Lothar
--
Lothar Kimmeringer E-Mail: (e-mail address removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
M

markspace

Mário said:
ByteArrayOutputStream data = new ByteArrayOutputStream(); ....
Does someone know why this does not generate a POST request?

Even after correcting the calls as Lothar suggested, I don't see how
this is going to generate a POST request, or that sending binary data to
an HTTP socket is going to be a good idea.

Are you sure this is really what you want to do? Could you explain a
bit more what you are trying to accomplish?
 
M

Mário

Because you don't ask the connection to do that. Call
connection.getInputStream() and the request will be sent.
BTW: You can skip the writing to the ByteArrayOutputStream
and initialized the ObjectOutputStream with
connection.getOutputStream()
Calling of os.close() isn't needed, either.

Regards, Lothar
--
Lothar Kimmeringer                E-Mail: (e-mail address removed)
               PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
                 questions!

Nice!
I've added the lines

InputStream in = connection.getInputStream();
in.close();

and it now works: the Date object is sent to the servlet.

But, why is the binary data sent in a subsequent packet? What can be
done to send a single packet for the POST request?

Regards,
Mário
 
L

Lothar Kimmeringer

Mário wrote:

[how to do it]
and it now works: the Date object is sent to the servlet.

Good to hear.
But, why is the binary data sent in a subsequent packet? What can be
done to send a single packet for the POST request?

The why is hidden in the internals of HttpUrlConnection but
I can think of a reason. In general the data being sent by
Raw POST is much more than the header. So sending the header
then checking if at that point of time the server already sent
something back (e.g. a 404) reduces the needed bandwith. Sending
megabytes of data just to realize that the server already denied
the request at the beginning might get frustrating ;-)

Authorative answers can be found by checking the source of
the affected classes of the JVM.


Regards, Lothar
--
Lothar Kimmeringer E-Mail: (e-mail address removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
M

Mike Schilling

markspace said:
Even after correcting the calls as Lothar suggested, I don't see how
this is going to generate a POST request, or that sending binary data
to an HTTP socket is going to be a good idea.

There's no problem sending binary data via HTTP, so long as that's what both
the client and server expect. For neatness, an appropriate content-type
should be set, say, "application/octet-stream".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top