Implementation of GET request in java in abc

 avatar
user_9237437122
java
3 years ago
1.1 kB
1
Indexable
package getrequest;

import java.net.*;
import java.io.*;
public class GetRequest {
    public static void httpGetRequest() throws IOException {
        
        URL url = new URL("https://jsonplaceholder.typicode.com/posts/");
        HttpURLConnection connect = (HttpURLConnection) url.openConnection();
        connect.setRequestMethod("GET");
        
        int responseCode = connect.getResponseCode();
        System.out.println("Response code: "+responseCode+ " & Response message: "+connect.getResponseMessage());
        
        if(responseCode == HttpURLConnection.HTTP_OK ){
        BufferedReader read = new BufferedReader(new InputStreamReader(connect.getInputStream()));
        
        StringBuffer str = new StringBuffer();
        String store = null;
        
        while((store = read.readLine())!= null)
            str.append(store); 
        read.close();
            System.out.println("GET Response: "+str.toString());
        }
    }
     public static void main(String[] args) throws IOException{
       httpGetRequest();
    }
}