javarfc2396

Encode and Decode rfc2396 URLs


What is the best way to encode URL strings such that they are rfc2396 compliant and to decode a rfc2396 compliant string such that for example %20 is replaced with a space character?

edit: URLEncoder and URLDecoder classes do not encode/decode rfc2396 compliant URLs, they encode to a MIME type of application/x-www-form-urlencoded which is used to encode HTML form parameter data.


Solution

  • Use the URI class as follows:

    URI uri = new URI("http", "//www.someurl.com/has spaces in url", null);
    URL url = uri.toURL();
    

    or if you want a String:

    String urlString = uri.toASCIIString();