Encoding URLs in Java

To encode a URL in Java, create a new instance of java.net.URI and then call toString() or toASCIIString() on it.

DO NOT use java.net.URLEncoder. It is for HTML form encoding, not encoding URLs, despite its name.

Each part of a URL must be escaped differently, so use the multi-argument constructors for URI where you have multiple parts you want to stick together. You can pass in null for arguments you want to omit, and it works the way you’d hope.

This program:

class EncodeUrls
{
    public static void main( String[] args ) throws java.net.URISyntaxException
    {
        url( "http", "example.com", "/x/y/x", "x=y", "pos" );

        url(
            "http",
            "exa\uD83D\uDCA9mple.com",
            "/x/\uD83D\uDCA9/x",
            "x=\uD83D\uDCA9",
            "po\uD83D\uDCA9"
        );
    }

    private static void url(
        String scheme,
        String host,
        String path,
        String query,
        String fragment
    )
    throws java.net.URISyntaxException
    {
        java.net.URI uri = new java.net.URI(
            scheme, host, path, query, fragment );

        System.out.println( "." + uri.toString() );
        System.out.println( "." + uri.toASCIIString() );
        System.out.println();
    }
}

prints:

.http://example.com/x/y/x?x=y#pos
.http://example.com/x/y/x?x=y#pos

.http://exa💩mple.com/x/💩/x?x=💩#po💩
.http://exa%F0%9F%92%A9mple.com/x/%F0%9F%92%A9/x?x=%F0%9F%92%A9#po%F0%9F%92%A9

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.