初识HttpClient
- pom依赖
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
httpclient会依赖httpcore
(以下依赖配置从httpclient的pom.xml中拷贝得到的)
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<scope>compile</scope>
</dependency>
- 创建HttClient实例
CloseableHttpClient httpClient = HttpClients.createDefault();
- 创建Http请求
提供了8中请求方式:HttpGet
、HttpPost
、HttpPut
、HttpHead
、HttpOptions
、HttpPatch
、HttpTrace
、HttpDelete
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Accept", "application/json")