使用Apache HttpClient版本时:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
setHeader和addHeader有什么区别?
httpPost.addHeader("AuthenticationKey",authenticationKey);
httpPost.addHeader("Content-Type","application/json");
httpPost.setHeader("Cache-Control", "no-cache"); // HTTP 1.1
httpPost.setHeader("Pragma", "no-cache"); // HTTP 1.0
httpPost.setHeader("X-Requested-With", "XMLHttpRequest"); // mimics a browser REST request
最佳答案
正如您从文档中看到的:addHeader(String name, String value
向此消息添加标题。标头将添加到列表的末尾。setHeader(String name, String value
用相同的名称覆盖第一个标头。如果找不到具有给定名称的标头,则新标头将追加到列表的末尾。