Java HTTP Post post(String url, int timeout, Map header)

Here you can find the source of post(String url, int timeout, Map header)

Description

Method to make a POST request to the specified url

License

Open Source License

Parameter

Parameter Description
url URL to make the request on
timeout The request timeout
header The request header (can be null)

Exception

Parameter Description
IOException an exception

Return

Contents, each list entry represents a new line

Declaration

public static List<String> post(String url, int timeout, Map header) throws IOException 

Method Source Code


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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

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

public class Main {
    /**/*  w w  w . j a  v  a  2  s . com*/
     * Method to make a POST request to the specified url
     * @param url URL to make the request on
     * @param timeout The request timeout
     * @param header The request header (can be null)
     * @return Contents, each list entry represents a new line
     * @throws IOException
     */
    public static List<String> post(String url, int timeout, Map header) throws IOException {
        return request(url, timeout, "POST", header);
    }

    private static List<String> request(String url, int timeout, String method, Map header) throws IOException {
        if (!url.contains("http://") && !url.contains("https://"))
            url = "http://" + url;
        URL input = new URL(url);
        HttpURLConnection connection = (HttpURLConnection) input.openConnection();
        connection.setConnectTimeout(timeout);
        connection.setRequestMethod(method);
        if (header != null) {
            for (Object key : header.keySet())
                connection.setRequestProperty(String.valueOf(key), String.valueOf(header.get(key)));
        }
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        connection.connect();
        String temp;
        List<String> read = new ArrayList<String>();
        while ((temp = in.readLine()) != null)
            read.add(temp);
        in.close();
        return read;
    }

    /**
     * Method to make a GET request to the specified url
     * @param url URL to make the request on
     * @param timeout The request timeout
     * @param header The request header (can be null)
     * @return Contents, each list entry represents a new line
     * @throws IOException
     */
    public static List<String> get(String url, int timeout, Map header) throws IOException {
        return request(url, timeout, "GET", header);
    }
}

Related

  1. httpPost(String urlToRead, String post)
  2. httpPostText(String urlStr, String textString)
  3. post(String json, String url)
  4. post(String json, String url)
  5. post(String rawUrl, String body)
  6. post(String url, JsonNode body)
  7. post(String url, Map params, String charset)
  8. post(String url, String body, Map headers)
  9. post(String url, String payload)