Java HTTP Post requestPost(final String remoteUrl, final byte[] content)

Here you can find the source of requestPost(final String remoteUrl, final byte[] content)

Description

request Post

License

Open Source License

Declaration

public static byte[] requestPost(final String remoteUrl, final byte[] content) throws Exception 

Method Source Code

//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();
    }
}

Related

  1. postRaw(URL url, String params)
  2. postURL(final URL url, final String request)
  3. put(String url, String content)
  4. put(URL host, String endpoint, String customer, String name, String version, InputStream in)
  5. putPOST(HttpURLConnection h, String query)
  6. sendJson(URL url, String method, String data)
  7. sendJsonData(final HttpURLConnection connection, final String data)
  8. sendPost(String link, String urlParameters)
  9. sendPost(String url, String data)