Here you can find the source of doPost(final URL url, final Map
Parameter | Description |
---|---|
url | a parameter |
parameters | a parameter |
encode | a parameter |
Parameter | Description |
---|---|
IOException | an exception |
public static InputStream doPost(final URL url, final Map<String, Object> parameters, final boolean encode) throws IOException
//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(); } }