Implementation of java GET request

Implementation of java GET request
 avatar
user_9332925630
java
2 years ago
1.1 kB
6
Indexable
Never
//Implementation of GET request
package httpreq;
import java.net.*;
import java.io.*;

public class GetRequest {
    
    public static void httpGetRequest() throws IOException {
        
        URL url = new URL("https://ar-aman.herokuapp.com/users");
        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();
    }
}