Joaquim d'Souza
Joaquim d'Souza

Reputation: 1516

How to fix illegal characters in URL in Java?

I would like a function that detects invalid characters in a URL and replaces them with their encoded equivalent. E.G.:

ensureValidUrl("http://example.com/invalid url/") // "http://example.com/invalid%20url/"

I had tried URLEncoder.encode, but this also encodes the protocol, which I don't want.

Upvotes: 4

Views: 1727

Answers (1)

Harshal Parekh
Harshal Parekh

Reputation: 6017

static String getValidURL(String invalidURLString){
    try {
        // Convert the String and decode the URL into the URL class
        URL url = new URL(URLDecoder.decode(invalidURLString, StandardCharsets.UTF_8.toString()));

        // Use the methods of the URL class to achieve a generic solution
        URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
        // return String or
        // uri.toURL() to return URL object
        return uri.toString();
    } catch (URISyntaxException | UnsupportedEncodingException | MalformedURLException ignored) {
        return null;
    }
}

Using a combination of URI and URL classes, your solution can be achieved. More on URL and URI and Charsets.

Usage:

System.out.println(getValidURL("http://example.com/invalid url/"));
// http://example.com/invalid%20url/

Upvotes: 3

Related Questions