URLConnection send data to server

A

autogoor

I am using the following code to upload a large binary data to a http
server.

URL u = new URL(urlString);
URLConnection c = u.openConnection();
c.setDoOutput(true);
c.setDoInput(true);
c.setUseCaches(false);
// set some request headers
c.setRequestProperty(
"Connection",
"Keep-Alive");
// get codebase of the this (the applet) to use for referer
c.setRequestProperty(
"HTTP_REFERER",
codebase);
c.setRequestProperty(
"Content-Type",
"multipart/form-data; boundary=" + boundary);

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

for (int i = 0; i < 1000; i++){
byte[] data = new byte[1000];
data = ....
dstream.write(dataq,0,1000);
}
dstream.writeBytes("\r\n--" + boundary + "--\r\n");
dstream.flush();
dstream.close();

I found that the data was never sent. If I put the following line after
the code above, however, the data got sent. Why?

c.getInputStream();
 
K

Kevin McMurtrie

I am using the following code to upload a large binary data to a http
server.

URL u = new URL(urlString);
URLConnection c = u.openConnection();
c.setDoOutput(true);
c.setDoInput(true);
c.setUseCaches(false);
// set some request headers
c.setRequestProperty(
"Connection",
"Keep-Alive");
// get codebase of the this (the applet) to use for referer
c.setRequestProperty(
"HTTP_REFERER",
codebase);
c.setRequestProperty(
"Content-Type",
"multipart/form-data; boundary=" + boundary);

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

for (int i = 0; i < 1000; i++){
byte[] data = new byte[1000];
data = ....
dstream.write(dataq,0,1000);
}
dstream.writeBytes("\r\n--" + boundary + "--\r\n");
dstream.flush();
dstream.close();

I found that the data was never sent. If I put the following line after
the code above, however, the data got sent. Why?

c.getInputStream();

It could be confused about which HTTP method to use. See
HttpURLConnection, or whatever it's called. Also, if you turn on input,
you must read the input. The socket can get reset if you don't, and
then each end gets an error that discards buffered data.
 

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,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top