HttpClient的定义
HTTP 协议可能是现在 Internet 上使用得最多、最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源。虽然在 JDK 的 java net包中已经提供了访问 HTTP 协议的基本功能,但是对于大部分应用程序来说,JDK 库本身提供的功能还不够丰富和灵活。HttpClient 是 Apache Jakarta Common 下的子项目,用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。HttpClient 已经应用在很多的项目中,比如 Apache Jakarta 上很著名的另外两个开源项目 Cactus 和 HTMLUnit 都使用了 HttpClient。现在HttpClient最新版本为 HttpClient 4.5 .6
在java代理内部可以使用httpClient发起http请求访问服务器获取资源(工具API)。
HttpClient入门案例
引入jar包
<!--添加httpClient jar包 -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
编辑测试API
package com.jt;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.jupiter.api.Test;
import java.io.IOException;
public class HttpClientTest {
/**
* 要求:java代码内部,获取百度的页面
* 实现步骤:
* 1.确定目标地址:https://www.baidu.com/
* 2.创建httpclient客户端对象
* 3.创建请求类型
* 4.发起http请求,并且获取响应的结果,之后判断状态码是否为200,
* 如果等于200则请求正确。
* 5.如果请求正确则动态获取响应值信息,之后进行数据的再次加工。
*/
@Test
public void httpClientTest() throws IOException {
//确定目标地址
String url="https://www.baidu.com/";
//创建httpclient客户端对象
HttpClient httpClient= HttpClients.createDefault();
//创建请求类型
HttpGet httpGet=new HttpGet(url);
//发起http请求
HttpResponse httpResponse=httpClient.execute(httpGet);
//判断状态码是否为200
if(httpResponse.getStatusLine().getStatusCode()==200){
//表示用户请求正确。获取返回值数据
HttpEntity httpEntity=httpResponse.getEntity();
//将对象转换为字符串类型,并拼接UTF-8编码以防乱码
String result= EntityUtils.toString(httpEntity,"UTF-8");
System.out.println(result);
}
}
}
HttpClient加强案例
案例需求
用户通过网址 http://www.jt.com/getItems 要求采用httpClient方式,获取jt-manage中的商品信息 之后以json串的形式展现.
jt-web服务器访问jt-manage时的网址 http://manage.jt.com/getItems.
编辑前台HttpClientController
package com.jt.controller;
import com.jt.pojo.Item;
import com.jt.service.HttpClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class HttpClientController {
@Autowired
private HttpClientService httpClientService;
/**
* 获取后端manage中的商品数据信息
*/
@RequestMapping("/getItems")
public List<Item> getItems(){
return httpClientService.getItems();
}
}
编辑前台HttpClientService
package com.jt.service;
import com.jt.pojo.Item;
import com.jt.util.ObjectMapperUtil;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Service
public class HttpClientServiceImpl implements HttpClientService{
@Override
public List<Item> getItems() {
List<Item> itemList=new ArrayList<>();
//1.定义远程访问地址
String url="http://manage.jt.com/getItems";
HttpClient httpClient= HttpClients.createDefault();
HttpGet httpGet=new HttpGet(url);
try {
HttpResponse httpResponse = httpClient.execute(httpGet);
if(httpResponse.getStatusLine().getStatusCode()==200){
String result = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
//result是jt-manage为jt-web返回的List<Item>的JSON串
if(!StringUtils.isEmpty(result)){
itemList=ObjectMapperUtil.toObj(result,itemList.getClass());
}
}
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
return itemList;
}
}
编辑后台HttpClientController
package com.jt.web.controller;
import com.jt.pojo.Item;
import com.jt.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class HttpClientController {
@Autowired
private ItemService itemService;
//url请求地址:http://manage.jt.com/getItems
@RequestMapping("/getItems")
public List<Item> getItems(){
return itemService.getItems();
}
}
编辑后台HttpClientService
@Override
public List<Item> getItems() {
return itemMapper.selectList(null);
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。