void send(byte[] body, String mediaType) throws IOException { // intentionally not closing the connection, so as to use keep-alives HttpURLConnection connection = (HttpURLConnection) endpoint().openConnection(); connection.setConnectTimeout(connectTimeout()); connection.setReadTimeout(readTimeout()); connection.setRequestMethod("POST"); connection.addRequestProperty("Content-Type", mediaType); if (compressionEnabled()) { connection.addRequestProperty("Content-Encoding", "gzip"); ByteArrayOutputStream gzipped = new ByteArrayOutputStream(); GZIPOutputStream compressor = new GZIPOutputStream(gzipped); try { compressor.write(body); } finally { compressor.close(); } body = gzipped.toByteArray(); } connection.setDoOutput(true); connection.setFixedLengthStreamingMode(body.length); connection.getOutputStream().write(body); skipAllContent(connection); }