Java HTTP Post URLPost(String strUrl, Map map)

Here you can find the source of URLPost(String strUrl, Map map)

Description

URL Post

License

Apache License

Declaration

public static List URLPost(String strUrl, Map map) throws IOException 

Method Source Code


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

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Main {
    public static List URLPost(String strUrl, Map map) throws IOException {
        String content = "";
        content = getUrl(map);// w ww  .j  a va2s .  com
        String totalURL = null;
        if (strUrl.indexOf("?") == -1) {
            totalURL = strUrl + "?" + content;
        } else {
            totalURL = strUrl + "&" + content;
        }
        URL url = new URL(strUrl);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setDoInput(true);
        con.setDoOutput(true);
        con.setAllowUserInteraction(false);
        con.setUseCaches(false);
        con.setRequestMethod("POST");
        con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=GBK");
        BufferedWriter bout = new BufferedWriter(new OutputStreamWriter(con.getOutputStream()));
        bout.write(content);
        bout.flush();
        bout.close();
        BufferedReader bin = new BufferedReader(new InputStreamReader(con.getInputStream()), 1048576);
        List result = new ArrayList();
        for (;;) {
            String line = bin.readLine();
            if (line == null) {
                break;
            }
            result.add(line);
        }
        return result;
    }

    private static String getUrl(Map map) {
        if ((map == null) || (map.keySet().size() == 0)) {
            return "";
        }
        StringBuffer url = new StringBuffer();
        Set keys = map.keySet();
        for (Iterator i = keys.iterator(); i.hasNext();) {
            String key = String.valueOf(i.next());
            if (map.containsKey(key)) {
                Object val = map.get(key);
                String str = val != null ? val.toString() : "";
                try {
                    str = URLEncoder.encode(str, "GBK");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                url.append(key).append("=").append(str).append("&");
            }
        }
        String strURL = "";
        strURL = url.toString();
        if ("&".equals(strURL.charAt(strURL.length() - 1))) {
            strURL = strURL.substring(0, strURL.length() - 1);
        }
        return strURL;
    }
}

Related

  1. sendPost(String url, String urlParameters)
  2. sendPost(String webURL, HashMap postOptions)
  3. sendPostRequest(Reader data, URL endpoint, Writer output)
  4. sendPostRequest(Reader data, URL endpoint, Writer output)
  5. sendPostRequest(String requestURL, Map params)
  6. writeBody(HttpURLConnection connection, String body)
  7. writeContent(HttpURLConnection urlConn, String content)
  8. writeFileFromUrl(URL sourceUrl, File targetFile)
  9. writeString(HttpURLConnection conn, String content, String charsetName)