如何编码 UTF-8 CloseableHttpClient 请求
How to encode UTF-8 CloseableHttpClient request
我有一个简单的POST:
String url = "https://mysite";
try (CloseableHttpClient httpClient = HttpClients.custom().build()) {
URIBuilder uriBuilder = new URIBuilder(url);
HttpPost request = new HttpPost();
List<NameValuePair> nameValuePairs = new ArrayList<>(params);
request.setEntity(new UrlEncodedFormEntity(nameValuePairs, StandardCharsets.UTF_8.name()));
String encodedAuthorization = URLEncoder.encode(data, StandardCharsets.UTF_8.name());
request.addHeader("Authorization", encodedAuthorization);
try (CloseableHttpResponse response = httpClient.execute(request)) {
我必须支持 UTF-8 编码,而编码 UrlEncodedFormEntity
还不够,但不清楚 必须 做什么,有几个可用的选项
使用uriBuilder.setCharset
:
HttpPost request = new HttpPost(uriBuilder.setCharset(StandardCharsets.UTF_8)).build());
使用http.protocol.content-charset
参数:
HttpPost request = new HttpPost(uriBuilder.setParameter("http.protocol.content-charset", "UTF-8").build());
或者只是添加 Content-Type"` header:
request.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
或使用request.getParams():
request.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_1);
request.getParams().setParameter("http.protocol.content-charset", "UTF-8");
或者我忽略的其他明显解决方案?
// Method to encode a string value using `UTF-8` encoding scheme
private static String encodeValue(String value) {
try {
return URLEncoder.encode(value, StandardCharsets.UTF_8.toString());
} catch (UnsupportedEncodingException ex) {
throw new RuntimeException(ex.getCause());
}
}
public static void main(String[] args) {
String baseUrl = "https://www.google.com/search?q=";
String query = "Hellö Wörld@Java";
String encodedQuery = encodeValue(query); // Encoding a query string
String completeUrl = baseUrl + encodedQuery;
System.out.println(completeUrl);
}
}
输出
https://www.google.com/search?q=Hell%C3%B6+W%C3%B6rld%40Java
我认为这可能是解决方案。
为了正确编码,我开始使用 List<NameValuePair>
中的参数而不是 URIBuilder
然后使用
编码
URLEncodedUtils.format(nameValuePairs, StandardCharsets.UTF_8.name());
我有一个简单的POST:
String url = "https://mysite";
try (CloseableHttpClient httpClient = HttpClients.custom().build()) {
URIBuilder uriBuilder = new URIBuilder(url);
HttpPost request = new HttpPost();
List<NameValuePair> nameValuePairs = new ArrayList<>(params);
request.setEntity(new UrlEncodedFormEntity(nameValuePairs, StandardCharsets.UTF_8.name()));
String encodedAuthorization = URLEncoder.encode(data, StandardCharsets.UTF_8.name());
request.addHeader("Authorization", encodedAuthorization);
try (CloseableHttpResponse response = httpClient.execute(request)) {
我必须支持 UTF-8 编码,而编码 UrlEncodedFormEntity
还不够,但不清楚 必须 做什么,有几个可用的选项
使用uriBuilder.setCharset
:
HttpPost request = new HttpPost(uriBuilder.setCharset(StandardCharsets.UTF_8)).build());
使用http.protocol.content-charset
参数:
HttpPost request = new HttpPost(uriBuilder.setParameter("http.protocol.content-charset", "UTF-8").build());
或者只是添加 Content-Type"` header:
request.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
或使用request.getParams():
request.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_1);
request.getParams().setParameter("http.protocol.content-charset", "UTF-8");
或者我忽略的其他明显解决方案?
// Method to encode a string value using `UTF-8` encoding scheme
private static String encodeValue(String value) {
try {
return URLEncoder.encode(value, StandardCharsets.UTF_8.toString());
} catch (UnsupportedEncodingException ex) {
throw new RuntimeException(ex.getCause());
}
}
public static void main(String[] args) {
String baseUrl = "https://www.google.com/search?q=";
String query = "Hellö Wörld@Java";
String encodedQuery = encodeValue(query); // Encoding a query string
String completeUrl = baseUrl + encodedQuery;
System.out.println(completeUrl);
}
}
输出
https://www.google.com/search?q=Hell%C3%B6+W%C3%B6rld%40Java
我认为这可能是解决方案。
为了正确编码,我开始使用 List<NameValuePair>
中的参数而不是 URIBuilder
然后使用
编码URLEncodedUtils.format(nameValuePairs, StandardCharsets.UTF_8.name());