I want encode an URL in a way that special characters like " " (space) are substituted in the correct way (%20 in the case of spaces). No one of the solution I found online is working as expected.
I tryed using apache commons:
import org.apache.commons.lang.StringEscapeUtils;
public class MyTest {
public static void main(String[] args) {
String bla="http://www.bla.com/bla.php?par1=bla bla bla";
System.out.println(StringEscapeUtils.escapeHtml(bla));
}
}
But it returns:
http://www.bla.com/bla.php?par1=bla bla bla
I tryed with java.net.URL:
import java.net.MalformedURLException;
import java.net.URL;
public class MyTest {
public static void main(String[] args) throws MalformedURLException {
String bla="http://www.bla.com/bla.php?par1=bla bla bla";
URL url = new URL(bla);
System.out.println(url);
}
}
But it returns:
http://www.bla.com/bla.php?par1=bla bla bla
I would expect:
http://www.bla.com/bla.php?par1=bla%20bla%20bla
Is there any way to do that?