Here you can find the source of requestPost(final String remoteUrl, final byte[] content)
public static byte[] requestPost(final String remoteUrl, final byte[] content) throws Exception
//package com.java2s; /******************************************************************************* * Copyright (c) 2013 Jens Kristian Villadsen. * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Public License v3.0 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/gpl.html/*from w w w .j av a 2 s . com*/ * * Contributors: * Jens Kristian Villadsen - Lead developer, owner and creator ******************************************************************************/ import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.zip.GZIPInputStream; import java.util.zip.Inflater; import java.util.zip.InflaterInputStream; public class Main { public static byte[] requestPost(final String remoteUrl, final byte[] content) throws Exception { final byte[] buffer = new byte[1024]; final HttpURLConnection connection = (HttpURLConnection) new URL(remoteUrl).openConnection(); connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.getOutputStream().write(content); connection.connect(); if (connection.getResponseCode() >= HttpURLConnection.HTTP_UNAUTHORIZED) throw new Exception("HTTP Error Response Code: " + connection.getResponseCode()); // obtain the encoding returned by the server final String encoding = connection.getContentEncoding(); InputStream inputStream = null; // create the appropriate stream wrapper based on the encoding type if (encoding != null && encoding.equalsIgnoreCase("gzip")) { inputStream = new GZIPInputStream(connection.getInputStream()); } else if (encoding != null && encoding.equalsIgnoreCase("deflate")) { inputStream = new InflaterInputStream(connection.getInputStream(), new Inflater(true)); } else { inputStream = connection.getInputStream(); } final ByteArrayOutputStream os = new ByteArrayOutputStream(); try { int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { os.write(buffer, 0, bytesRead); } } finally { os.flush(); os.close(); if (inputStream != null) { inputStream.close(); } } return os.toByteArray(); } }