欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

HttpClient

程序员文章站 2022-07-10 18:06:58
...

业务需求

说明:当做某些操作时,可能会对数据进行业务加工,之后由服务器与服务器之间形同通讯。
HttpClient
HttpClient

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(2015-09-11)
总结: 在java代理内部可以使用httpClient发起http请求访问服务器获取资源.(工具API)

入门案例

1.引入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.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
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 testGet() throws IOException {
        String url = "https://www.jd.com/";
        HttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        HttpResponse httpResponse = httpClient.execute(httpGet);
          if(httpResponse.getStatusLine().getStatusCode() == 200) {
            //表示用户请求正确
            //获取返回值数据
            HttpEntity httpEntity = httpResponse.getEntity();
            String result = EntityUtils.toString(httpEntity, "UTF-8");
            System.out.println(result);
        }
    }
}

HttpClient加强案例

要求:用户通过http://www.jt.com/getItems 要求采用httpClient方式,采取js-manage中的商品信息之后json串的形式展现

编辑前台 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