import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class Client { public static void get() throws IOException { URL url = new URL("https://jsonplaceholder.typicode.com/users/"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.connect(); InputStream is = conn.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); StringBuffer sb = new StringBuffer(); String line = br.readLine(); while (line != null) { sb.append(line); sb.append(System.lineSeparator()); line = br.readLine(); } conn.disconnect(); System.out.println(sb.toString()); } public static void post() throws IOException { URL url = new URL("https://jsonplaceholder.typicode.com/users/"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-type", "application/json"); conn.setDoOutput(true); String jsonInputString = "{\"name\":\"morpheus\",\"job\":\"leader\"}"; conn.getOutputStream().write(jsonInputString.getBytes()); conn.getOutputStream().flush(); conn.getOutputStream().close(); conn.connect(); InputStream is = conn.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); StringBuffer sb = new StringBuffer(); String line = br.readLine(); while (line != null) { sb.append(line); sb.append(System.lineSeparator()); line = br.readLine(); } conn.disconnect(); System.out.println(sb.toString()); } public static void put() throws IOException { URL url = new URL("https://jsonplaceholder.typicode.com/users/1"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("PUT"); conn.setRequestProperty("Content-type", "application/json"); conn.setDoOutput(true); String jsonInputString = "{\"name\":\"morpheus\",\"job\":\"zion resident\"}"; conn.getOutputStream().write(jsonInputString.getBytes()); conn.getOutputStream().flush(); conn.getOutputStream().close(); conn.connect(); InputStream is = conn.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); StringBuffer sb = new StringBuffer(); String line = br.readLine(); while (line != null) { sb.append(line); sb.append(System.lineSeparator()); line = br.readLine(); } conn.disconnect(); System.out.println(sb.toString()); } public static void delete() throws IOException { URL url = new URL("https://jsonplaceholder.typicode.com/users/1"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("DELETE"); conn.connect(); InputStream is = conn.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); StringBuffer sb = new StringBuffer(); String line = br.readLine(); while (line != null) { sb.append(line); sb.append(System.lineSeparator()); line = br.readLine(); } conn.disconnect(); System.out.println(sb.toString()); } }