如果我已经设置了 StringEntity 的内容类型,还需要添加 Content-Type header 吗?

Do I need to add Content-Type header if I already set the StringEntity's content type?

我需要对 body 中带有 UTF-8 JSON 的服务器执行 POST 请求:

public HttpRequest createRequest() {
    HttpPost req = new HttpPost(API_URL);
    StringEntity stringEntity = new StringEntity(notification.toString(), Charset.forName("UTF-8"));         
    stringEntity.setContentType(ContentType.APPLICATION_JSON.toString());
    req.setEntity(stringEntity);
    return req;
}

stringEntity 的内容类型和字符集是否自动添加到请求中?

或者我是否必须将 header 添加到 req

req.addHeader("Content-Type", "application/json; charset=UTF-8");

不,你没有。内容元数据 headers 由 RequestContent 协议拦截器自动生成。