文章目录
一、HttpClient
1.1、 前台系统访问后台接口的方式
两个系统间如何互相访问?两个tomcat上的项目如何互相访问
采用HttpClient实现跨系统的接口调用。
1.2、 什么是HttpClient
官网:http://hc.apache.org/index.html
现在也叫:HttpComponents
特点:
- HttpClient别名:HttpComponents
- HttpClient可以发送get、post、put、delete、…等请求
1.3、 HttpClient入门案例
pom.xml和application.properties文件
导入maven坐标
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.4</version>
</dependency>
1.3.1、 发起Get请求
- 流程
1.创建一个客户端 CloseableHttpClient
2.创建一个get方法请求实例 HttpGet
3.发送请求 execute
4.获取响应的头信息
5.获取响应的主题内容
6.关闭响应对象
使用HttpClient发起Get请求的案例代码:
/**
* 使用HttpClient发起Get请求,请求百度
*/
public class DoGETParam {
public static void main(String[] args) throws Exception {
// 创建Httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
// 创建URI对象,并且设置请求参数
URI uri = new URIBuilder("http://www.baidu.com/s").setParameter("wd", "java").build();
// 创建http GET请求
HttpGet httpGet = new HttpGet(uri);