Added URLEncoder section
This commit is contained in:
parent
aa3b40f307
commit
1da595e08a
1 changed files with 33 additions and 0 deletions
33
README.md
33
README.md
|
@ -102,3 +102,36 @@ java -jar lib/build/libs/urlencoder-*all.jar "%#okékÉȢ" # -> %25%23o
|
||||||
|
|
||||||
java -jar lib/build/libs/urlencoder-*.all.jar -d "a%20test%20%26" # -> a test &
|
java -jar lib/build/libs/urlencoder-*.all.jar -d "a%20test%20%26" # -> a test &
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Why not simply use `java.net.URLEncoder`?
|
||||||
|
|
||||||
|
Apart for being quite inefficient, some URL components encoded with `URLEncoder.encode` might not be able to be properly decoded.
|
||||||
|
|
||||||
|
For example, a simply search query such as:
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
val u = URLEncoder.encode("foo +bar", StandardCharsets.UTF_8)
|
||||||
|
```
|
||||||
|
|
||||||
|
would be encoded as:
|
||||||
|
|
||||||
|
```
|
||||||
|
foo+%28bar
|
||||||
|
```
|
||||||
|
|
||||||
|
Trying to decode it with [Spring](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/util/UriUtils.html#decode(java.lang.String,java.lang.String)), for example:
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
UriUtils.decode(u, StandardCharsets.UTF_8))
|
||||||
|
```
|
||||||
|
|
||||||
|
would return:
|
||||||
|
|
||||||
|
```
|
||||||
|
foo++bar
|
||||||
|
```
|
||||||
|
|
||||||
|
Unfortunately, decoding with [Uri.decode](https://developer.android.com/reference/android/net/Uri#decode(java.lang.String)) on Android, [decodeURI](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURI) in Javascript, etc. would yield the exact same result.
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue