Here you can find the source of postCall(JsonObject json, String url)
public static JsonObject postCall(JsonObject json, String url)
//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(); } }