{Java}REST API

 

https://www.delftstack.com/ja/howto/java/call-rest-api-in-java/
https://techbooster.org/android/application/6812/
https://qiita.com/Hyman1993/items/bdf5500acff17f2b4840


notepad HTTPGet.java

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

public class HTTPGet {

    public static void main(String args) throws Exception {

        URL url = new URL("http://localhost:9090/webadmin/denodo-scheduler-admin/public/api/projects?uri=//localhost:8000");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");

        conn.setRequestProperty("Authorization","Basic YWRtaW46YWRtaW4=");
        conn.setRequestProperty("Content-Type","application/json");

        conn.connect();

        int responseCode = conn.getResponseCode();
        System.out.println("GET Response Code : " + responseCode);

        if (responseCode == conn.HTTP_OK) {
            InputStream in = conn.getInputStream();
            InputStreamReader inReader = new InputStreamReader(in);
            BufferedReader bufReader = new BufferedReader(inReader);

            StringBuffer result = new StringBuffer();
            String line = null;

            while ( (line = bufReader.readLine() ) != null) {
                result.append(line);
            }
            bufReader.close();
            inReader.close();
            in.close();
            System.out.println( result.toString() );
        } else {
            System.out.println("Error found");
        }
    }
}


java HTTPGet.java

----------------------


notepad HTTPPost.java


import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class HTTPPost {

    public static void main(String args) throws MalformedURLException, IOException {

        URL url = new URL("http://localhost:9090/webadmin/denodo-scheduler-admin/public/api/projects?uri=//localhost:8000");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");

        conn.setRequestProperty("Authorization","Basic YWRtaW46YWRtaW4=");
        conn.setRequestProperty("Content-Type","application/json");
        
        conn.setDoOutput(true);
        conn.setDoInput(true);
        
        conn.connect();
        
        String body = "{\"name\": \"project116\", \"description\": \"description of project116\"}";

        OutputStream out = conn.getOutputStream();
        OutputStreamWriter outWriter = new OutputStreamWriter(out);
        BufferedWriter bufWriter  = new BufferedWriter(outWriter);
        bufWriter.write(body);
        bufWriter.close();
        outWriter.close();
        out.close();

        int responseCode = conn.getResponseCode();
        System.out.println("POST Response Code : " + responseCode);

        if (responseCode == conn.HTTP_CREATED) {
            InputStream in= conn.getInputStream();
            InputStreamReader inReader = new InputStreamReader(in);
            BufferedReader bufReader = new BufferedReader(inReader);

            StringBuffer result = new StringBuffer();
            String line = null;

            while ( (line = bufReader.readLine() ) != null) {
                result.append(line);
            }
            bufReader.close();
            inReader.close();
            in.close();
            System.out.println( result.toString() );
        } else {
            System.out.println("Error found");
        }
    }
}

 

java HTTPPost.java