HTTP URL Address Encoding in Java?

The class can help; in the documentation of URL you find


Note, the URI class does perform escaping of its component fields in certain circumstances. The recommended way to manage the encoding and decoding of URLs is to use an URI


Use one of the constructors with more than one argument, like:


URI uri = new URI(



    "/booksearch/first book.pdf",


URL url = uri.toURL();

//or String request = uri.toString();

(the single-argument constructor of URI does NOT escape illegal characters)