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;@RestControllerpublic 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;@Servicepublic 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;@RestControllerpublic 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); }