如果HTTP POST请求头中含有非ISO-8859-1字符,需要将字符串进行编码。可以使用Java中的URLEncoder对值进行编码,然后将其放置到请求头中。示例如下:
import java.net.URLEncoder;
String charset = "UTF-8";
String requestBody = "example=中文";
String encodedRequestBody = URLEncoder.encode(requestBody, charset);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=" + charset);
connection.setRequestProperty("Content-Length", String.valueOf(encodedRequestBody.getBytes().length));
connection.setDoOutput(true);
try (OutputStream output = connection.getOutputStream()) {
output.write(encodedRequestBody.getBytes(charset));
}
这样就可以在HTTP POST请求头中包含非ISO-8859-1字符了。
上一篇:包含非度量数据的事实表
下一篇:包含非字母字符的字符串数量