java url connection, wait for data being sent through the outputstream

Posted by Mateu on Stack Overflow See other posts from Stack Overflow or by Mateu
Published on 2011-01-07T10:44:09Z Indexed on 2011/01/07 13:53 UTC
Read the original article Hit count: 210

Filed under:
|
|
|
|

I'm writting a java class that tests uploading speed connection to a server. I want to check how many data can be send in 5 seconds.

I've written a class which creates a URL, creates a connection, and sends data trough the outPutStream. There is a loop where I writte data to the stream for 5 seconds. However I'm not able to see when data has been send (I writte data to the output stream, but data is not send yet). How can I wait untill data is really sent to the server? Here goes my code (which does not work):

URL u = new URL(url)
HttpURLConnection uc = (HttpURLConnection) u.openConnection();
uc.setDoOutput(true);
uc.setDoInput(true);
uc.setUseCaches(false);
uc.setDefaultUseCaches(false);
uc.setRequestMethod("POST");
uc.setRequestProperty("Content-Type", "application/octet-stream");
uc.connect();
st.start();

// Send the request
OutputStream os = uc.getOutputStream();
//This while is incorrect cause it does not wait for data beeing sent
while (st.getElapsedTime() < miliSeconds) {
    os.write(buffer);
    os.flush();
    st.addSize(buffer.length);

}
os.close();

Thanks

© Stack Overflow or respective owner

Related posts about java

Related posts about url