Index

HTTP GET

private static String doGet(String url) throws Exception {
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();

        con.setRequestMethod("GET");
        con.setRequestProperty("Accept", "application/json");

        int responseCode = con.getResponseCode();
        System.out.println("GET URL : " + url + " status " + responseCode);
  
        return readInputStream(con.getInputStream());
}

HTTP POST

private static String doPost(String url) throws Exception {
	URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();

        con.setRequestMethod("POST");
        con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
        con.setDoOutput(true);

        String params = "user=root&password=passw";
        writeOutputStream(params, con.getOutputStream());

        int responseCode = con.getResponseCode();
	System.out.println("POST URL : " + url + " status " + responseCode);
	return readInputStream(con.getInputStream());
}

add basic authentication header

public void addBasicAuthentication(HttpURLConnection con, String user, String password) {
        String b64 = new String(Base64.getEncoder().encode(new String(user + ":" + password).getBytes()));
        con.setRequestProperty("Authorization", "Basic " + b64);
    }