Reputation: 1516
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
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