In Java, I want to convert this:
https%3A%2F%2Fmywebsite%2Fdocs%2Fenglish%2Fsite%2Fmybook.do%3Frequest_type
To this:
https://mywebsite/docs/english/site/mybook.do&request_type
This is what I have so far:
class StringUTF { public static void main(String[] args) { try{ String url = "https%3A%2F%2Fmywebsite%2Fdocs%2Fenglish%2Fsite%2Fmybook.do" + "%3Frequest_type%3D%26type%3Dprivate"; System.out.println(url+"Hello World!------->" + new String(url.getBytes("UTF-8"),"ASCII")); } catch(Exception E){ } } }
But it doesn't work right. What are these %3A
and %2F
formats called and how do I convert them?
public class URLDecoder extends Object. Utility class for HTML form decoding. This class contains static methods for decoding a String from the application/x-www-form-urlencoded MIME format. The conversion process is the reverse of that used by the URLEncoder class.
URL Encoding converts reserved, unsafe, and non-ASCII characters in URLs to a format that is universally accepted and understood by all web browsers and servers. It first converts the character to one or more bytes. Then each byte is represented by two hexadecimal digits preceded by a percent sign ( % ) - (e.g. %xy ).
This does not have anything to do with character encodings such as UTF-8 or ASCII. The string you have there is URL encoded. This kind of encoding is something entirely different than character encoding.
Try something like this:
try { String result = java.net.URLDecoder.decode(url, StandardCharsets.UTF_8.name()); } catch (UnsupportedEncodingException e) { // not going to happen - value came from JDK's own StandardCharsets }
Java 10 added direct support for Charset
to the API, meaning there's no need to catch UnsupportedEncodingException:
String result = java.net.URLDecoder.decode(url, StandardCharsets.UTF_8);
Note that a character encoding (such as UTF-8 or ASCII) is what determines the mapping of characters to raw bytes. For a good intro to character encodings, see this article.
The string you've got is in application/x-www-form-urlencoded
encoding.
Use URLDecoder to convert it to Java String.
URLDecoder.decode( url, "UTF-8" );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With