我有一个uri喜欢
http://localhost/?name=foo&value=bar
我用
org.apache.http.client.utils.URLEncodedUtils.parse(URI uri, String encoding)
获取NameValuePairs的列表,它运行良好。但现在我还需要解析中文字符的可能性,例如:
http://localhost/?name=生产者&value=单车
但URLEncodedUtilsparse无法正确解析这些字符。如何检索它们并再次获取NameValuePairs列表?
答案 0 :(得分:1)
您可以尝试这样:
String query1 = URLEncoder.encode("生产者", "UTF-8");
String query2 = URLEncoder.encode("单车", "UTF-8");
String url = "http://localhost/?name=" + query1 + "&value=" + query2;
答案 1 :(得分:0)
我遇到了类似的情况。 URLEncodedUtils必须与StringEntity一起使用。 UrlEncodedFormEntity已损坏。
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpPost postRequest = new HttpPost(
URL);
List<NameValuePair> param_data = new ArrayList<>();
param_data.add(new BasicNameValuePair("name", [STRING_FOREIGN CHARS));
String s = URLEncodedUtils.format(param_data, java.nio.charset.Charset.forName("UTF-8"));
StringEntity entity = new StringEntity(s, java.nio.charset.Charset.forName("UTF-8"));
postRequest.setEntity(entity); // don't use postRequest.setEntity(new UrlEncodedFormEntity(param_data));
HttpResponse response = httpClient.execute(postRequest);
..[snipped] do whatever you want with response