Java URL Connection httpPost(String urlString, String postPath, Map keyValuePairs)

Here you can find the source of httpPost(String urlString, String postPath, Map keyValuePairs)

Description

http Post

License

Open Source License

Declaration

public static List<String> httpPost(String urlString, String postPath, Map<String, String> keyValuePairs) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;

import java.util.List;
import java.util.Map;

import java.util.Map.Entry;

public class Main {
    public static List<String> httpPost(String urlString, String postPath, Map<String, String> keyValuePairs) {
        List<String> ret = new ArrayList<String>();
        try {//from  ww  w  .j a v  a 2 s  .c  om
            // first strip away any ending "/" character from urlString
            //   and any beginning "/" from postPath
            String firstPart;
            if (urlString.substring(urlString.length() - 1).compareTo("/") == 0) {
                firstPart = urlString.substring(0, urlString.length() - 1);
            } else {
                firstPart = urlString;
            }
            String secondPart;
            if (postPath.substring(0).compareTo("/") == 0) {
                secondPart = postPath.substring(1, postPath.length() - 1);
            } else {
                secondPart = postPath.substring(0, postPath.length() - 1);
            }
            String completePath = firstPart + "/" + secondPart;
            URL url = new URL(completePath);
            // Construct data
            String data = null;
            for (Entry<String, String> entry : keyValuePairs.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                if (data == null) {
                    data = URLEncoder.encode(key, "UTF-8") + "=" + URLEncoder.encode(value, "UTF-8");
                } else {
                    data += "&" + URLEncoder.encode(key, "UTF-8") + "=" + URLEncoder.encode(value, "UTF-8");
                }
            }

            // Send data
            URLConnection conn = url.openConnection();
            conn.setDoOutput(true);
            OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
            wr.write(data);
            wr.flush();

            // Get the response
            BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = rd.readLine()) != null) {
                ret.add(line);
            }
            wr.close();
            rd.close();
        } catch (Exception e) {
            return null;
        }
        return ret;
    }
}

Related

  1. getURLInputStream(final URL url)
  2. getUrlSource(String url)
  3. getURLStream(URL url, int level)
  4. getWebPageHtmlContent(String url)
  5. getWebsiteContents(URL url)
  6. imageFromUrl(String url)
  7. isJarDirectory(JarURLConnection conn)
  8. isOnline(String url)
  9. isUrlReachable(String url)