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

Introduction

The java.net.URLConnection class in Java is used to establish a connection to a URL and perform various operations such as sending HTTP requests, receiving HTTP responses, and handling cookies. While the Oracle tutorial provides a basic overview of how to use URLConnection to fire a GET request and read the response, it lacks in-depth information on how to perform more advanced operations such as making POST requests, setting request headers, reading response headers, dealing with cookies, submitting HTML forms, and uploading files.

In this article, we will delve into these advanced topics and explore how to effectively use java.net.URLConnection to handle a wide range of HTTP requests.

Table of Contents

Making a GET Request

To make a GET request using URLConnection, you need to follow these steps:

import java.net.*;
import java.io.*;

public class GetRequestExample {
    public static void main(String[] args) throws Exception {
        // Create a URL object
        URL url = new URL("https://example.com/api");
        
        // Open a connection to the URL
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        
        // Set the request method to GET
        conn.setRequestMethod("GET");
        
        // Read the response
        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        StringBuilder response = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            response.append(line);
        }
        reader.close();
        
        // Print the response
        System.out.println(response.toString());
        
        // Disconnect the connection
        conn.disconnect();
    }
}
        

Making a POST Request

To make a POST request using URLConnection, you need to follow these steps:

import java.net.*;
import java.io.*;

public class PostRequestExample {
    public static void main(String[] args) throws Exception {
        // Create a URL object
        URL url = new URL("https://example.com/api");
        
        // Open a connection to the URL
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        
        // Set the request method to POST
        conn.setRequestMethod("POST");
        
        // Enable output streaming
        conn.setDoOutput(true);
        
        // Create the request payload
        String payload = "key1=value1&key2=value2";
        
        // Write the payload to the connection's output stream
        OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
        writer.write(payload);
        writer.flush();
        
        // Read the response
        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        StringBuilder response = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            response.append(line);
        }
        reader.close();
        
        // Print the response
        System.out.println(response.toString());
        
        // Disconnect the connection
        conn.disconnect();
    }
}
        

Setting Request Headers

To set request headers using URLConnection, you can use the setRequestProperty method. Here's an example:

import java.net.*;
import java.io.*;

public class RequestHeadersExample {
    public static void main(String[] args) throws Exception {
        // Create a URL object
        URL url = new URL("https://example.com/api");
        
        // Open a connection to the URL
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        
        // Set the request method to GET
        conn.setRequestMethod("GET");
        
        // Set the request headers
        conn.setRequestProperty("User-Agent", "Mozilla/5.0");
        conn.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
        
        // Read the response
        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        StringBuilder response = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            response.append(line);
        }
        reader.close();
        
        // Print the response
        System.out.println(response.toString());
        
        // Disconnect the connection
        conn.disconnect();
    }
}
        

Reading Response Headers

To read response headers using URLConnection, you can use the getHeaderFields method. Here's an example:

import java.net.*;
import java.io.*;

public class ResponseHeadersExample {
    public static void main(String[] args) throws Exception {
        // Create a URL object
        URL url = new URL("https://example.com/api");
        
        // Open a connection to the URL
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        
        // Set the request method to GET
        conn.setRequestMethod("GET");
        
        // Read the response
        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        StringBuilder response = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            response.append(line);
        }
        reader.close();
        
        // Print the response headers
        Map> headers = conn.getHeaderFields();
        for (Map.Entry<String, List> entry : headers.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
        
        // Print the response body
        System.out.println(response.toString());
        
        // Disconnect the connection
        conn.disconnect();
    }
}
        

Handling Cookies

To handle cookies using URLConnection, you can use the CookieManager class and its associated CookieHandler implementation. Here's an example:

import java.net.*;
import java.io.*;
import java.net.CookieHandler;
import java.net.CookieManager;

public class CookieExample {
    public static void main(String[] args) throws Exception {
        // Enable cookie management
        CookieManager cookieManager = new CookieManager();
        CookieHandler.setDefault(cookieManager);
        
        // Create a URL object
        URL url = new URL("https://example.com/login");
        
        // Open a connection to the URL
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        
        // Set the request method to POST
        conn.setRequestMethod("POST");
        
        // Enable output streaming
        conn.setDoOutput(true);
        
        // Create the request payload
        String payload = "username=myuser&password=mypassword";
        
        // Write the payload to the connection's output stream
        OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
        writer.write(payload);
        writer.flush();
        
        // Read the response
        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        StringBuilder response = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            response.append(line);
        }
        reader.close();
        
        // Disconnect the connection
        conn.disconnect();
        
        // Print the cookies
        cookieManager.getCookieStore().getCookies().forEach(System.out::println);
    }
}
        

Submitting HTML Forms

To submit HTML forms using URLConnection, you need to create a payload with the form data and send it as the request body. Here's an example:

import java.net.*;
import java.io.*;

public class FormSubmitExample {
    public static void main(String[] args) throws Exception {
        // Create a URL object
        URL url = new URL("https://example.com/login");
        
        // Open a connection to the URL
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        
        // Set the request method to POST
        conn.setRequestMethod("POST");
        
        // Enable output streaming
        conn.setDoOutput(true);
        
        // Create the request payload
        String payload = "username=myuser&password=mypassword";
        
        // Write the payload to the connection's output stream
        OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
        writer.write(payload);
        writer.flush();
        
        // Read the response
        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        StringBuilder response = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            response.append(line);
        }
        reader.close();
        
        // Disconnect the connection
        conn.disconnect();
        
        // Print the response
        System.out.println(response.toString());
    }
}
        

Uploading Files

To upload files using URLConnection, you need to set the request method to POST, enable output streaming, set the appropriate request headers, and send the file content as the request body. Here's an example:

import java.net.*;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class FileUploadExample {
    public static void main(String[] args) throws Exception {
        // Create a URL object
        URL url = new URL("https://example.com/upload");
        
        // Open a connection to the URL
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        
        // Set the request method to POST
        conn.setRequestMethod("POST");
        
        // Enable output streaming
        conn.setDoOutput(true);
        
        // Set the request headers
        conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=---------------------------1234567890");
        
        // Create the request payload
        String boundary = "-----------------------------1234567890";
        String CRLF = "\r\n";
        
        StringBuilder payload = new StringBuilder();
        
        payload.append("--").append(boundary).append(CRLF);
        payload.append("Content-Disposition: form-data; name=\"file\"; filename=\"file.txt\"").append(CRLF);
        payload.append("Content-Type: text/plain").append(CRLF);
        payload.append(CRLF);
        
        // Read the file content
        Path filePath = Paths.get("path/to/file.txt");
        byte[] fileContent = Files.readAllBytes(filePath);
        
        // Append the file content to the payload
        payload.append(new String(fileContent)).append(CRLF);
        
        payload.append("--").append(boundary).append("--");
        
        // Write the payload to the connection's output stream
        OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
        writer.write(payload.toString());
        writer.flush();
        
        // Read the response
        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        StringBuilder response = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            response.append(line);
        }
        reader.close();
        
        // Disconnect the connection
        conn.disconnect();
        
        // Print the response
        System.out.println(response.toString());
    }
}