java url connection, wait for data being sent through the outputstream
- by Mateu
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