关于java:HttpClient简单介绍

12次阅读

共计 4025 个字符,预计需要花费 11 分钟才能阅读完成。

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);
    }

页面成果展现

正文完
 0