Reputation: 103
I'm using some API in JAVA.
I need to use Korean text in URI, and request is the String variable.
If I set request = "안녕하세요";
and i use the code:
final URI uri = new URIBuilder().setScheme("https").setHost(server + "api.net").setPath("/api/" + "/" + **request**).setParameters(param).build(). ;
If I use this, I can see this following result:
https://api.net/api/%EC%95%88%EB%85%95%ED%95%98%EC%84%B8%EC%9A%94?api_key
I already tried to use this code:
final URI uri = new URIBuilder().setScheme("https").setHost(server + "api.net").setPath("/api/" + "/" + **URLEncoder.encode(request, "UTF-8")**).setParameters(param).build(). ;
but I am getting the same result.
How can I solve this problem?
Upvotes: 2
Views: 4383
Reputation: 2667
Your URI has been encoded. As like URL, you can't use special characters, if you want to retrieve request
string, you must decode that specific part of your URI, something like this:
String result = java.net.URLDecoder.decode(uri.getPath(), "UTF-8");
System.out.println(result); // this will print "/api/안녕하세요"
The RFC 3986, Uniform Resource Identifier (URI): Generic Syntax confirms it:
A URI is a sequence of characters from a very limited set: the letters of the basic Latin alphabet, digits, and a few special characters.
Later it says:
Percent-encoded octets [...] may be used within a URI to represent characters outside the range of the US-ASCII coded character set if this representation is allowed by the scheme or by the protocol element in which the URI is referenced.
Which is the bunch of characters that you are getting.
Hope you find this useful.
Upvotes: 2