Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do urlencode, rawurlencode, urldecode, rawurldecode in Java?

How do you do RAW/URL encode/decode in JAVA?

Got something which asked URLencoded or RAWURLencoded need to be sent. After looking here and there, did not find any url/rawurl encoder/decoder available. How do you make it?

Example:

String webBrowser = "how=WebRSS&what=Tandory chicken is nice with rice, lets cook for my honey";
String encoded = null;

/**
 * Output: how%3DWebRSS%26what%3DTandory+chicken+is+nice+with+rice%2C+lets+cook+for+my+honey
 */
urlencode(webBrowser); 

/**
 * Output: how%3DWebRSS%26what%3DTandory%20chicken%20is%20nice%20with%20rice%2C%20lets%20cook%20for%20my%20honey
 */
rawurlencode(webBrowser);

postIt(encoded); //!!

1 Answers

For urlencode/urldecode (the application/x-www-form-urlencoded variants), you can use URLEncoder and URLDecoder.

For the "raw" versions, you can use the URI class, which performs this task automatically.

like image 157
Artefacto Avatar answered Mar 07 '26 14:03

Artefacto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!