杰瑞科技汇

java urlconnection

Of course! Here is a comprehensive guide to using java.net.URLConnection in Java, covering everything from the basics to best practices and modern alternatives.

java urlconnection-图1
(图片来源网络,侵删)

What is java.net.URLConnection?

java.net.URLConnection is an abstract class in Java that represents a communication link between the Java application and a resource specified by a URL. It's the foundation for all types of URL-based communication, including HTTP, HTTPS, FTP, and more.

When you call openConnection() on a URL object, the JVM returns an instance of a concrete subclass (like HttpURLConnection for HTTP/HTTPS) that is appropriate for the protocol.

Key Concepts

  • URL: Represents the Uniform Resource Locator (e.g., https://www.example.com/api/data).
  • URLConnection: The abstract class for the connection. You rarely instantiate this directly.
  • HttpURLConnection: The concrete subclass used for HTTP and HTTPS connections. It's the most common one you'll work with. It extends URLConnection and provides methods specific to HTTP (like setRequestMethod, getResponseCode).
  • Request: The data you send to the server (e.g., GET parameters, POST data, headers).
  • Response: The data you receive from the server (e.g., status code, headers, content).

Basic Workflow for an HTTP GET Request

Here is the standard step-by-step process for making a simple GET request.

Step 1: Create a URL Object

First, create a URL object from the string representation of the resource you want to access.

java urlconnection-图2
(图片来源网络,侵删)
URL url = new URL("https://jsonplaceholder.typicode.com/todos/1");

Step 2: Open a Connection

Call the openConnection() method on the URL object. This returns a URLConnection, which you should typically cast to HttpURLConnection to access HTTP-specific features.

HttpURLConnection connection = (HttpURLConnection) url.openConnection();

Step 3: Configure the Request

Set the request method (e.g., GET, POST) and any necessary request headers.

connection.setRequestMethod("GET");
// You can add headers like this:
connection.setRequestProperty("User-Agent", "MyJavaApp/1.0");
connection.setRequestProperty("Accept", "application/json");

Step 4: Connect and Get the Response Code

Call connect() to send the request (though it's often implicitly called when you try to read the response). Then, get the HTTP status code to check if the request was successful (e.g., 200 OK).

int responseCode = connection.getResponseCode();
System.out.println("Response Code: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) { // success
    // Proceed to read the response
} else {
    System.out.println("GET request failed");
}

Step 5: Read the Response Stream

If the request was successful, you can read the data from the input stream. Crucially, you must use a try-with-resources block to ensure the stream is always closed, preventing resource leaks.

java urlconnection-图3
(图片来源网络,侵删)
try (BufferedReader in = new BufferedReader(
        new InputStreamReader(connection.getInputStream()))) {
    String inputLine;
    StringBuilder response = new StringBuilder();
    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    System.out.println("Response Body: " + response.toString());
}

Step 6: Disconnect (Optional but good practice)

It's good practice to disconnect the connection to free up system resources, especially in long-running applications.

connection.disconnect();

Complete Example: GET Request

This example fetches a JSON object from a public test API.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class UrlConnectionGetExample {
    private static final String USER_AGENT = "Mozilla/5.0";
    public static void main(String[] args) throws IOException {
        String urlString = "https://jsonplaceholder.typicode.com/todos/1";
        URL url = new URL(urlString);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        // 1. Set request method and headers
        connection.setRequestMethod("GET");
        connection.setRequestProperty("User-Agent", USER_AGENT);
        connection.setRequestProperty("Accept", "application/json");
        System.out.println("Sending 'GET' request to URL : " + urlString);
        System.out.println("Response Code : " + connection.getResponseCode());
        // 2. Check if the response is successful
        if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            // 3. Read the response
            try (BufferedReader in = new BufferedReader(
                    new InputStreamReader(connection.getInputStream()))) {
                String inputLine;
                StringBuilder response = new StringBuilder();
                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                // Print result
                System.out.println("Response Body: " + response.toString());
            }
        } else {
            System.out.println("GET request did not work.");
        }
        // 4. Disconnect
        connection.disconnect();
    }
}

Making a POST Request

Making a POST request is similar, but with a few key differences:

  1. Set the request method to POST.
  2. You must enable output with setDoOutput(true).
  3. You write the request body (data) to the output stream.
  4. You must set the Content-Type header to match the data you are sending (e.g., application/x-www-form-urlencoded or application/json).

Complete Example: POST Request

This example sends a JSON payload to create a new "todo" item.

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
public class UrlConnectionPostExample {
    public static void main(String[] args) throws IOException {
        String urlString = "https://jsonplaceholder.typicode.com/todos";
        String jsonPayload = "{\"title\":\"Buy groceries\",\"body\":\"Milk, Eggs, Bread\",\"userId\":1}";
        URL url = new URL(urlString);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        // 1. Set request method and headers
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/json; utf-8");
        connection.setRequestProperty("Accept", "application/json");
        connection.setDoOutput(true); // This is crucial for POST
        // 2. Write the request body
        try (OutputStream os = connection.getOutputStream()) {
            byte[] input = jsonPayload.getBytes(StandardCharsets.UTF_8);
            os.write(input, 0, input.length);
        }
        // 3. Get the response code
        int responseCode = connection.getResponseCode();
        System.out.println("POST Response Code : " + responseCode);
        // 4. Read the response
        if (responseCode == HttpURLConnection.HTTP_CREATED) { // 201 Created
            try (BufferedReader br = new BufferedReader(
                    new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
                StringBuilder response = new StringBuilder();
                String responseLine;
                while ((responseLine = br.readLine()) != null) {
                    response.append(responseLine.trim());
                }
                System.out.println("Response Body: " + response.toString());
            }
        } else {
            System.out.println("POST request did not work.");
        }
        connection.disconnect();
    }
}

Handling Timeouts and Errors

Connections can fail for many reasons (slow network, server down). You should always handle potential IOExceptions and set timeouts to prevent your application from hanging indefinitely.

// Set timeouts in milliseconds
connection.setConnectTimeout(5000); // 5 seconds
connection.setReadTimeout(5000);   // 5 seconds
// Handle potential IO exceptions
try {
    // ... connection logic ...
} catch (IOException e) {
    System.err.println("An error occurred: " + e.getMessage());
    e.printStackTrace();
}

Modern Alternatives: HttpClient

While HttpURLConnection is powerful and built into the Java standard library, it has some drawbacks:

  • Verbose and verbose.
  • Can be clunky for complex scenarios.
  • The API is not as modern or intuitive as newer alternatives.

Since Java 11, the standard library includes a new, modern, and much more powerful HTTP client: java.net.http.HttpClient.

Why Prefer HttpClient?

  • Asynchronous & Non-Blocking: Supports asynchronous operations out-of-the-box, which is crucial for building high-performance, non-blocking applications.
  • Fluent API: The API is cleaner and more expressive.
  • Simplicity: It's much easier to write concise code for both simple and complex requests.
  • WebSocket Support: Has built-in support for WebSockets.

Example with HttpClient (Java 11+)

Here is the same GET request, but using the modern HttpClient.

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class HttpClientExample {
    public static void main(String[] args) throws IOException, InterruptedException {
        String uriString = "https://jsonplaceholder.typicode.com/todos/1";
        // 1. Create an HttpClient
        HttpClient client = HttpClient.newHttpClient();
        // 2. Create an HttpRequest
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(uriString))
                .header("Accept", "application/json")
                .GET() // This is the default, but explicit is good
                .build();
        // 3. Send the request and get the response
        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
        // 4. Process the response
        System.out.println("Status Code: " + response.statusCode());
        System.out.println("Response Body: " + response.body());
    }
}

Summary: HttpURLConnection vs. HttpClient

Feature HttpURLConnection java.net.http.HttpClient (Java 11+)
Availability Java 1.1 (Standard Library) Java 11+ (Standard Library)
API Style Verbose, stateful Fluent, modern, functional
Asynchronous Support Clunky, requires manual threads and handling Built-in, easy-to-use sendAsync() method
Simplicity More boilerplate code (streams, readers, try-catch) Much cleaner and more concise
Performance Good for simple blocking tasks Excellent, especially with its non-blocking capabilities
Recommendation Use for legacy code or projects on Java 8 or earlier. Strongly recommended for all new projects on Java 11+.

Conclusion

  • HttpURLConnection is a solid, reliable choice if you are working with older Java versions or need a dependency-free solution. It's powerful but can be verbose.
  • HttpClient is the modern, recommended standard for all new Java development (11+). It offers a superior developer experience, better performance, and a more intuitive API.

For any new project, you should default to using java.net.http.HttpClient. However, understanding HttpURLConnection is still valuable as it's widely used in existing codebases and Java 8 environments.

分享:
扫描分享到社交APP
上一篇
下一篇