IMHO is a HttpURLConnectionreally bad API. But processing input and output messages as streams is a way to work efficiently with large amounts of data. I think all the other answers (currently 5!) Are correct. Some questions open:
At what point is the message really sent to the server? On the connection? On getInputStream? When trying to read from a stream?
, (, , -,...) . connect, , . getResponseCode() getInputStream(). (BTW getResponseCode() getInputStream(), (, 404), getInputStream , getErrorStream().)
, , ?
getOutputStream(), . () . .
-- ?
. keep-alive. HttpURLConnection , - .
API HttpURLConnection, API , DavidWebb. DavidWebb :
Webb webb = Webb.create();
String result = webb.post("http://my-server/path/resource")
.header("auth-token", myAuthToken)
.body(myBody)
.ensureSuccess()
.asString()
.getBody();