Home > Java > javaTutorial > How to Properly Encode HTTP URLs in Java Using the java.net.URI Class?

How to Properly Encode HTTP URLs in Java Using the java.net.URI Class?

Linda Hamilton
Release: 2024-12-18 12:42:18
Original
220 people have browsed it

How to Properly Encode HTTP URLs in Java Using the java.net.URI Class?

How to Properly Encode HTTP URLs in Java

When working with URLs in Java, it is crucial to encode them correctly to ensure that they are interpreted accurately by the server. While the java.net.URLEncoder can assist in HTML form encoding, it falls short when it comes to HTTP URL encoding. This article explores how to effectively encode HTTP URLs using the java.net.URI class.

The java.net.URI class provides a way to manipulate and encode URI and URL components. The single-argument constructor of URI does not escape illegal characters, leaving you with errors like the one described in the question. To address this issue, you should utilize a constructor with multiple arguments, such as:

URI uri = new URI("http", "search.barnesandnoble.com", "/booksearch/first book.pdf", null);
URL url = uri.toURL();
Copy after login

This approach effectively escapes illegal characters, giving you the desired result of:

http://search.barnesandnoble.com/booksearch/first%20book.pdf
Copy after login

It is important to note that this encoding only affects illegal characters, not non-ASCII characters. For a String containing only US-ASCII characters, you can use the toASCIIString method:

URI uri = new URI("http", "search.barnesandnoble.com", "/booksearch/é", null);
String request = uri.toASCIIString();
Copy after login

This is particularly useful for URLs with queries that contain non-ASCII characters:

URI uri = new URI("http", "www.google.com", "/ig/api", "weather=São Paulo", null);
String request = uri.toASCIIString();
Copy after login

By leveraging the java.net.URI class, you can effectively encode HTTP URLs, ensuring that they are handled correctly by the server and resulting in accurate data retrieval.

The above is the detailed content of How to Properly Encode HTTP URLs in Java Using the java.net.URI Class?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template