Java an http utility class to send getpost request

www.i‮editfig‬a.com

In Java, you can use the HttpURLConnection class to send HTTP requests to a web server. Here's an example utility class that you can use to send GET and POST requests:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class HttpUtility {
    
    public static String sendGetRequest(String requestUrl) throws Exception {
        URL url = new URL(requestUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line;
        StringBuilder response = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            response.append(line);
        }
        reader.close();
        connection.disconnect();
        
        return response.toString();
    }
    
    public static String sendPostRequest(String requestUrl, Map<String, String> parameters) throws Exception {
        URL url = new URL(requestUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setDoOutput(true);
        
        OutputStream outputStream = connection.getOutputStream();
        StringBuilder builder = new StringBuilder();
        for (Map.Entry<String, String> entry : parameters.entrySet()) {
            builder.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
        }
        builder.deleteCharAt(builder.length() - 1);
        outputStream.write(builder.toString().getBytes());
        outputStream.flush();
        outputStream.close();
        
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line;
        StringBuilder response = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            response.append(line);
        }
        reader.close();
        connection.disconnect();
        
        return response.toString();
    }
}

The sendGetRequest() method sends a GET request to the specified URL and returns the response as a String. The sendPostRequest() method sends a POST request to the specified URL with the specified parameters, and returns the response as a String.

Here's an example of how to use these methods:

public class Main {
    public static void main(String[] args) {
        try {
            String response = HttpUtility.sendGetRequest("https://www.example.com");
            System.out.println(response);
            
            Map<String, String> parameters = new HashMap<>();
            parameters.put("param1", "value1");
            parameters.put("param2", "value2");
            response = HttpUtility.sendPostRequest("https://www.example.com", parameters);
            System.out.println(response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

In this example, we first call sendGetRequest() to send a GET request to "https://www.example.com" and print the response to the console. Then, we create a Map object containing some parameters, and call sendPostRequest() to send a POST request to "https://www.example.com" with those parameters, and print the response to the console. If there is an exception thrown, we print the stack trace to the console.