Search code examples
javasolrspecial-charactersurl-encoding

Parse Chinese characters with URLEncodedUtils in Java


I have a uri like

http://localhost/?name=foo&value=bar

And I use

org.apache.http.client.utils.URLEncodedUtils.parse(URI uri, String encoding)

to get a list of NameValuePairs, and it works nicely. But now I have need also the possibility to parse Chinese charecters, e.g.:

http://localhost/?name=生产者&value=单车

But URLEncodedUtilsparse fails to parse these characters correctly. How can I retrieve them and get a list of NameValuePairs again?


Solution

  • You can try like this:

    String query1 = URLEncoder.encode("生产者", "UTF-8");
    String query2 = URLEncoder.encode("单车", "UTF-8");
    String url = "http://localhost/?name=" + query1 + "&value=" + query2;
    

    Also check the java.net.URLEncoder