- Parse and create URLs using the URL class
- Use URLConnection and HttpURLConnection for web requests
- Send GET and POST requests with data
- Handle redirects and encode URL parameters
Working with URLs and URLConnection
Need to fetch data from a web API or download a file? The URL and URLConnection classes handle HTTP communication without external libraries.
The URL Class
The URL class represents a web address:
import java.net.URL;
URL url = new URL("https://www.example.com:8080/path/to/resource?query=value#section");
// Parse URL components
System.out.println("Protocol: " + url.getProtocol()); // https
System.out.println("Host: " + url.getHost()); // www.example.com
System.out.println("Port: " + url.getPort()); // 8080
System.out.println("Path: " + url.getPath()); // /path/to/resource
System.out.println("Query: " + url.getQuery()); // query=value
System.out.println("Ref: " + url.getRef()); // section
System.out.println("File: " + url.getFile()); // /path/to/resource?query=value
// Default port (-1 means use default for protocol)
URL url2 = new URL("https://www.example.com/page");
System.out.println("Port: " + url2.getPort()); // -1
System.out.println("Default Port: " + url2.getDefaultPort()); // 443
Reading from a URL
Simple Reading
URL url = new URL("https://www.example.com");
// Open stream and read content
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(url.openStream()))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
Reading into a String
public static String readUrl(String urlString) throws IOException {
URL url = new URL(urlString);
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(url.openStream(), StandardCharsets.UTF_8))) {
StringBuilder content = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
content.append(line).append("\n");
}
return content.toString();
}
}
URLConnection
URLConnection provides more control over the connection:
URL url = new URL("https://api.example.com/data");
URLConnection connection = url.openConnection();
// Set timeouts
connection.setConnectTimeout(5000); // 5 seconds to connect
connection.setReadTimeout(10000); // 10 seconds to read
// Set request properties (headers)
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("User-Agent", "Java Client");
// Connect and read
connection.connect();
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(connection.getInputStream()))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
HttpURLConnection
For HTTP-specific operations, use HttpURLConnection:
URL url = new URL("https://api.example.com/data");
HttpURLConnection http = (HttpURLConnection) url.openConnection();
// Set request method
http.setRequestMethod("GET"); // GET, POST, PUT, DELETE, etc.
// Set headers
http.setRequestProperty("Accept", "application/json");
http.setRequestProperty("Authorization", "Bearer token123");
// Connect
http.connect();
// Check response
int responseCode = http.getResponseCode();
String responseMessage = http.getResponseMessage();
System.out.println("Response: " + responseCode + " " + responseMessage);
// Read response headers
Map<String, List<String>> headers = http.getHeaderFields();
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
// Read response body
if (responseCode == HttpURLConnection.HTTP_OK) {
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(http.getInputStream()))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
} else {
// Read error stream
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(http.getErrorStream()))) {
String line;
while ((line = reader.readLine()) != null) {
System.err.println(line);
}
}
}
// Always disconnect
http.disconnect();
Sending POST Data
URL url = new URL("https://api.example.com/users");
HttpURLConnection http = (HttpURLConnection) url.openConnection();
// Configure for POST
http.setRequestMethod("POST");
http.setDoOutput(true); // Enable writing to request body
http.setRequestProperty("Content-Type", "application/json");
// Write request body
String jsonBody = "{\"name\": \"John\", \"email\": \"john@example.com\"}";
try (OutputStream os = http.getOutputStream()) {
byte[] input = jsonBody.getBytes(StandardCharsets.UTF_8);
os.write(input, 0, input.length);
}
// Read response
int responseCode = http.getResponseCode();
System.out.println("Response Code: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_CREATED) { // 201
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(http.getInputStream()))) {
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
System.out.println("Response: " + response);
}
}
http.disconnect();
Sending Form Data
URL url = new URL("https://example.com/login");
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setRequestMethod("POST");
http.setDoOutput(true);
http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
// URL-encode form data
String formData = "username=" + URLEncoder.encode("john", StandardCharsets.UTF_8)
+ "&password=" + URLEncoder.encode("secret123", StandardCharsets.UTF_8);
try (OutputStream os = http.getOutputStream()) {
os.write(formData.getBytes(StandardCharsets.UTF_8));
}
// Handle response...
Downloading Files
public static void downloadFile(String urlString, String outputPath) throws IOException {
URL url = new URL(urlString);
HttpURLConnection http = (HttpURLConnection) url.openConnection();
int responseCode = http.getResponseCode();
if (responseCode != HttpURLConnection.HTTP_OK) {
throw new IOException("Server returned: " + responseCode);
}
// Get file size (may be -1 if unknown)
long fileSize = http.getContentLengthLong();
System.out.println("File size: " + (fileSize > 0 ? fileSize + " bytes" : "unknown"));
// Download file
try (InputStream in = http.getInputStream();
FileOutputStream out = new FileOutputStream(outputPath)) {
byte[] buffer = new byte[8192];
int bytesRead;
long totalRead = 0;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
totalRead += bytesRead;
if (fileSize > 0) {
int progress = (int) (totalRead * 100 / fileSize);
System.out.print("\rDownloading: " + progress + "%");
}
}
System.out.println("\nDownload complete!");
}
http.disconnect();
}
Handling Redirects
HttpURLConnection http = (HttpURLConnection) url.openConnection();
// Enable/disable automatic redirects
http.setInstanceFollowRedirects(true); // Default is true
// Or globally
HttpURLConnection.setFollowRedirects(false);
// Manual redirect handling
http.setInstanceFollowRedirects(false);
int status = http.getResponseCode();
if (status == HttpURLConnection.HTTP_MOVED_PERM
|| status == HttpURLConnection.HTTP_MOVED_TEMP
|| status == 307 || status == 308) {
String newUrl = http.getHeaderField("Location");
System.out.println("Redirecting to: " + newUrl);
// Follow redirect manually
http.disconnect();
URL redirectUrl = new URL(newUrl);
http = (HttpURLConnection) redirectUrl.openConnection();
}
URI vs URL
import java.net.URI;
import java.net.URL;
// URI is more general, URL is for accessing resources
URI uri = new URI("https://example.com/path?query=value");
URL url = uri.toURL();
// URI can represent non-retrievable identifiers
URI mailUri = new URI("mailto:user@example.com");
// mailUri.toURL() would throw MalformedURLException
// Building URIs safely (handles encoding)
URI safeUri = new URI("https", "example.com", "/search", "q=hello world", null);
System.out.println(safeUri); // https://example.com/search?q=hello%20world
URL Encoding
import java.net.URLEncoder;
import java.net.URLDecoder;
// Encode special characters
String encoded = URLEncoder.encode("Hello World!", StandardCharsets.UTF_8);
System.out.println(encoded); // Hello+World%21
// Decode
String decoded = URLDecoder.decode(encoded, StandardCharsets.UTF_8);
System.out.println(decoded); // Hello World!
// Build query string safely
String query = "name=" + URLEncoder.encode("John Doe", StandardCharsets.UTF_8)
+ "&city=" + URLEncoder.encode("New York", StandardCharsets.UTF_8);
Best Practices
- Always set timeouts to prevent hanging
- Use try-with-resources for streams
- Check response codes before reading
- Handle both success and error streams
- Encode URL parameters properly
- Disconnect HttpURLConnection when done
// Complete example with best practices
public static String fetchJson(String urlString) throws IOException {
URL url = new URL(urlString);
HttpURLConnection http = (HttpURLConnection) url.openConnection();
try {
http.setRequestMethod("GET");
http.setConnectTimeout(5000);
http.setReadTimeout(10000);
http.setRequestProperty("Accept", "application/json");
int status = http.getResponseCode();
InputStream stream = (status < 400)
? http.getInputStream()
: http.getErrorStream();
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(stream, StandardCharsets.UTF_8))) {
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
if (status >= 400) {
throw new IOException("HTTP " + status + ": " + response);
}
return response.toString();
}
} finally {
http.disconnect();
}
}
URL parses web addresses, URLConnection connects to URLs, HttpURLConnection handles HTTP methods and status codes, URLEncoder encodes parameters, URI handles general identifiers. Always set timeouts and handle errors. Encode special characters with URLEncoder. For new code, consider the modern HttpClient API (Java 11+).
