Java Utililty Methods HTTP Post

List of utility methods to do HTTP Post

Description

The list of methods to do HTTP Post are organized into topic(s).

Method

Stringpost(String url, String body, Map headers)
Send a post request
return fetch("POST", url, body, headers);
Stringpost(String url, String payload)
post
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(payload);
...
HttpURLConnectionpost(String url, String postdata, String cookie)
post
HttpURLConnection pHuc = getConnection(url);
pHuc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
pHuc.setRequestProperty("Origin", url.substring(0, url.indexOf("/")));
pHuc.setRequestProperty("Host", url.substring(0, url.indexOf("/")));
pHuc.setRequestProperty("Cookie", cookie);
try {
    pHuc.setRequestMethod("POST");
    pHuc.setDoOutput(true);
...
HttpURLConnectionPost(URL url, Map fields)
Post
String body;
body = encodeToForm(fields);
return Post(url, body);
Stringpost(URL url, Map values)
POST to the specified URL with the specified map of values.
return post(url, implode(values));
Stringpost(URL url, String content)
post
return post(url, content, "application/x-www-form-urlencoded");
InputStreampost(URL url, String contentType, byte[] data)
Sends HTTP POST request
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoOutput(true);
con.setRequestMethod("POST");
con.setRequestProperty("content-Type", contentType);
OutputStream out = con.getOutputStream();
try {
    out.write(data, 0, data.length);
    out.flush();
...
JsonObjectpostCall(JsonObject json, String url)
post Call
try {
    HttpURLConnection connection = getConnection(url);
    try (OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream())) {
        osw.write(json.toString());
        osw.flush();
    int responseCode = connection.getResponseCode();
    if (responseCode != 200) {
...
voidpostData(Reader data, URL endpoint, Writer output)
Reads data from the data reader and posts it to a server via a POST request.
HttpURLConnection urlc = null;
try {
    urlc = (HttpURLConnection) endpoint.openConnection();
    try {
        urlc.setRequestMethod("POST");
    } catch (ProtocolException e) {
        throw new RuntimeException("Shouldn't happen: HttpURLConnection doesn't support POST??", e);
    urlc.setDoOutput(true);
    urlc.setDoInput(true);
    urlc.setUseCaches(false);
    urlc.setAllowUserInteraction(false);
    urlc.setRequestProperty("Content-type", "text/xml; charset=" + "UTF-8");
    OutputStream out = urlc.getOutputStream();
    try {
        Writer writer = new OutputStreamWriter(out, "UTF-8");
        pipe(data, writer);
        writer.close();
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (Exception e) {
    InputStream in = urlc.getInputStream();
    try {
        Reader reader = new InputStreamReader(in);
        pipe(reader, output);
        reader.close();
    } finally {
        if (in != null)
            in.close();
} finally {
    if (urlc != null)
        urlc.disconnect();
StringpostForm(String url, Map params)
Post a form with parameters
return postForm(url, params, null);