Java HTTP Post postCall(JsonObject json, String url)

Here you can find the source of postCall(JsonObject json, String url)

Description

post Call

License

Open Source License

Declaration

public static JsonObject postCall(JsonObject json, String url) 

Method Source Code

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

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    public static JsonObject postCall(JsonObject json, String url) {
        try {/*from   w w w.ja v a 2s .c o  m*/
            HttpURLConnection connection = getConnection(url);
            try (OutputStreamWriter osw = new OutputStreamWriter(
                    connection.getOutputStream())) {
                osw.write(json.toString());
                osw.flush();
            }
            int responseCode = connection.getResponseCode();
            if (responseCode != 200) {
                System.out
                        .println("Sentinel errored out with response code "
                                + responseCode + " and response message "
                                + connection.getResponseMessage()
                                + " on JSON " + json.toString());
                return null;
            }

            try (InputStreamReader isr = new InputStreamReader(
                    connection.getInputStream())) {
                String response = getString(isr);
                JsonObject rootResponse = new JsonParser().parse(response)
                        .getAsJsonObject();
                return rootResponse;
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    private static HttpURLConnection getConnection(String url)
            throws IOException {
        HttpURLConnection conn = (HttpURLConnection) new URL(url)
                .openConnection();
        conn.setConnectTimeout(10 * 1000);
        conn.setReadTimeout(10 * 1000);
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");
        return conn;
    }

    private static String getString(InputStreamReader isr)
            throws IOException {
        BufferedReader br = new BufferedReader(isr);
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line + "\n");
        }
        br.close();
        return sb.toString();
    }
}

Related

  1. post(String url, String postdata, String cookie)
  2. Post(URL url, Map fields)
  3. post(URL url, Map values)
  4. post(URL url, String content)
  5. post(URL url, String contentType, byte[] data)
  6. postData(Reader data, URL endpoint, Writer output)
  7. postForm(String url, Map params)
  8. postHTTPQuery(String url, String urlParameters)
  9. postJSON(JSONObject job, HttpURLConnection conn)