请求头指定编码

This commit is contained in:
huangrun
2025-11-19 12:43:52 +08:00
parent 8d9b360654
commit fe3e8df37c

View File

@@ -7,6 +7,8 @@ import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.HttpClients;
import java.nio.charset.StandardCharsets;
/** /**
* @author * @author
* @date 2025年11月18日 * @date 2025年11月18日
@@ -22,8 +24,9 @@ public class HttpUtils {
// POST 请求 // POST 请求
HttpPost httpPost = new HttpPost(url); HttpPost httpPost = new HttpPost(url);
//HttpPost httpPost = new HttpPost("https://jsonplaceholder.typicode.com/posts"); //HttpPost httpPost = new HttpPost("https://jsonplaceholder.typicode.com/posts");
httpPost.setEntity(new StringEntity(message)); httpPost.setEntity(new StringEntity(message, StandardCharsets.UTF_8));
httpPost.setHeader("Content-Type", "application/json"); httpPost.setHeader("Content-Type", "application/json; charset=utf-8");
httpPost.setHeader("Accept", "application/json");
CloseableHttpResponse response = httpClient.execute(httpPost); CloseableHttpResponse response = httpClient.execute(httpPost);
log.info("---------------发送消息响应 -------{}",response); log.info("---------------发送消息响应 -------{}",response);
return response; return response;