How to use java.net.URLConnection to fire and handle HTTP requests

You can use the java.net.URLConnection class to fire and handle HTTP requests in Java. Here's an example of how you can use the URLConnection class to send a GET request and read the response:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class Main {
  public static void main(String[] args) {
    try {
      // Create a URL and open a connection
      URL url = new URL("http://www.example.com");
      URLConnection conn = url.openConnection();

      // Set the request method to GET
      conn.setRequestMethod("GET");

      // Read the response
      BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
      String line;
      while ((line = reader.readLine()) != null) {
        System.out.println(line);
      }
      reader.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

This code will send a GET request to the specified URL and print the response to the console.

You can also use the URLConnection class to send other types of HTTP requests, such as POST, PUT, and DELETE, by setting the request method using the setRequestMethod method and specifying the request body using the getOutputStream method.

I hope this helps! Let me know if you have any other questions.