Answers for "How to Implement GET and POST Requests With Java"

0

How to Implement GET and POST Requests With Java

public static void MyGETRequest() throws IOException {
    URL urlForGetRequest = new URL("https://jsonplaceholder.typicode.com/posts/1");
    String readLine = null;
    HttpURLConnection conection = (HttpURLConnection) urlForGetRequest.openConnection();
    conection.setRequestMethod("GET");
    conection.setRequestProperty("userId", "a1bcdef"); // set userId its a sample here
    int responseCode = conection.getResponseCode();


    if (responseCode == HttpURLConnection.HTTP_OK) {
        BufferedReader in = new BufferedReader(
            new InputStreamReader(conection.getInputStream()));
        StringBuffer response = new StringBuffer();
        while ((readLine = in .readLine()) != null) {
            response.append(readLine);
        } in .close();
        // print result
        System.out.println("JSON String Result " + response.toString());
        //GetAndPost.POSTRequest(response.toString());
    } else {
        System.out.println("GET NOT WORKED");
    }

}
Posted by: Guest on April-26-2022

Code answers related to "How to Implement GET and POST Requests With Java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language