Java URL Connection doPost(final URL url, final Map parameters, final boolean encode)

Here you can find the source of doPost(final URL url, final Map parameters, final boolean encode)

Description

do Post

License

Open Source License

Parameter

Parameter Description
url a parameter
parameters a parameter
encode a parameter

Exception

Parameter Description
IOException an exception

Return

InputStream

Declaration

public static InputStream doPost(final URL url, final Map<String, Object> parameters, final boolean encode)
        throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Manchester Centre for Integrative Systems Biology
 * University of Manchester//from  w  w  w  . j  ava 2s  . c om
 * Manchester M1 7ND
 * United Kingdom
 * 
 * Copyright (C) 2007 University of Manchester
 * 
 * This program is released under the Academic Free License ("AFL") v3.0.
 * (http://www.opensource.org/licenses/academic.php)
 *******************************************************************************/

import java.io.*;
import java.net.*;
import java.util.*;

public class Main {
    /**
     * 
     * @param url
     * @param parameters
     * @return InputStream
     * @throws IOException
     */
    public static InputStream doPost(final URL url, final Map<String, Object> parameters) throws IOException {
        return doPost(url, parameters, true);
    }

    /**
     * 
     * @param url
     * @param parameters
     * @param encode
     * @return InputStream
     * @throws IOException
     */
    public static InputStream doPost(final URL url, final Map<String, Object> parameters, final boolean encode)
            throws IOException {
        final URLConnection urlConn = url.openConnection();
        urlConn.setDoInput(true);
        urlConn.setDoOutput(true);
        urlConn.setUseCaches(false);
        urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); //$NON-NLS-1$ //$NON-NLS-2$

        OutputStream os = null;

        try {
            os = new DataOutputStream(urlConn.getOutputStream());
            os.write(getQueryString(parameters, encode).getBytes());
            os.flush();
        } finally {
            if (os != null) {
                os.close();
            }
        }

        return urlConn.getInputStream();
    }

    /**
     * 
     * @param parameters
     * @param encode
     * @return String
     * @throws UnsupportedEncodingException
     */
    public static String getQueryString(final Map<String, Object> parameters, final boolean encode)
            throws UnsupportedEncodingException {
        final String ENCODING = "UTF-8"; //$NON-NLS-1$
        final String EQUALS = "="; //$NON-NLS-1$
        final String AMPERSAND = "&"; //$NON-NLS-1$
        final String EQUALS_ENCODED = encode ? URLEncoder.encode(EQUALS, ENCODING) : EQUALS;
        final String AMPERSAND_ENCODED = encode ? URLEncoder.encode(AMPERSAND, ENCODING) : AMPERSAND;

        final StringBuffer queryString = new StringBuffer();

        for (Iterator<Map.Entry<String, Object>> iterator = parameters.entrySet().iterator(); iterator.hasNext();) {
            final Map.Entry<String, Object> entry = iterator.next();
            queryString.append(encode ? URLEncoder.encode(entry.getKey(), ENCODING) : entry.getKey());
            queryString.append(EQUALS_ENCODED);
            queryString.append(encode ? URLEncoder.encode(entry.getValue().toString(), ENCODING)
                    : entry.getValue().toString());
            queryString.append(AMPERSAND_ENCODED);
        }

        if (queryString.length() > 0) {
            queryString.setLength(queryString.length() - AMPERSAND_ENCODED.length());
        }

        return queryString.toString();
    }
}

Related

  1. createURLClassLoader(String dirPath)
  2. createURLConnection(String url)
  3. createWebStartDirectory(String name, String jarUrl)
  4. doGet(String targetURL)
  5. doGet(String url, String charset)
  6. doPost(String url, String params, String charset)
  7. findClasses(URL resource, String packageName, boolean annotated)
  8. findClasspathUrls(ClassLoader classLoader)
  9. findResourceInJarPackage(URL url, String packageName, String packageDirName, boolean recursive, List resources)