Java HTTP Post getHttpPOSTConnection(String urlStr, String charSet, Map props, Map params)

Here you can find the source of getHttpPOSTConnection(String urlStr, String charSet, Map props, Map params)

Description

get Http POST Connection

License

Apache License

Declaration

public static HttpURLConnection getHttpPOSTConnection(String urlStr, String charSet, Map<String, String> props,
            Map<String, String> params) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.IOException;

import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import java.util.Iterator;
import java.util.Map;

public class Main {
    public static HttpURLConnection getHttpPOSTConnection(String urlStr, String charSet, Map<String, String> props,
            Map<String, String> params) throws IOException {
        URL url = new URL(urlStr);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        for (Iterator iterator = props.keySet().iterator(); iterator.hasNext();) {
            String k = (String) iterator.next();
            connection.addRequestProperty(k, props.get(k));
        }//from  w  w w .  java  2s .  co  m

        connection.setRequestMethod("POST");
        connection.setDoOutput(true);
        OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
        StringBuilder sb = new StringBuilder();
        for (Iterator iterator = params.keySet().iterator(); iterator.hasNext();) {
            String name = (String) iterator.next();
            sb.append(name);
            sb.append("=");
            sb.append(URLEncoder.encode(params.get(name)));
            sb.append("\n");

        }
        System.out.println(sb.toString());
        writer.write(sb.toString());
        writer.close();

        return connection;

    }
}

Related

  1. doPost(String targetURL, String urlParameters, String contentType)
  2. doPost(String theURL, Map nameValuePairs)
  3. doPost(URL url, String body, String... args)
  4. executePost(String url, JSONObject data)
  5. executePostWithCredentials(String targetURL, String urlParameters, String userName, String password)
  6. httpGet(final String httpurl, final Map... requestProperties)
  7. httpPost(final String httpurl, final String data, final Map... requestProperties)
  8. httpPost(String httpUrl)
  9. httpPost(String url, StringBuffer data)