HttpClient
HttpClient 是用于发送 HTTP 请求和处理响应的工具包,在不同的编程语言中都有对应的实现,下面为你分别介绍 Java 和 Python 中的 HttpClient。
特点
- 功能丰富:支持多种 HTTP 方法(如 GET、POST、PUT、DELETE 等),能处理复杂的 HTTP 协议特性,像身份验证、代理设置、连接池管理等。
- 高度可定制:用户可以根据自身需求定制请求的各个方面,如设置请求头、超时时间、编码方式等。
- 稳定性高:经过大量的测试和实际应用验证,具备良好的稳定性和性能。
- 简洁易用:提供了简单直观的方法来发送各种 HTTP 请求,代码编写简洁。
- 自动处理多种情况:可以自动处理重定向、Cookie、SSL 验证等常见的 HTTP 相关问题。
- 广泛应用:在 Python 社区中被广泛使用,有丰富的文档和社区支持。
Jar包
<dependencies>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.13</version> <!-- 正确版本号 -->
    </dependency>
</dependencies>基本方法
- HttpClients:是工厂类,用于创建不同配置的- HttpClient实例。
- HttpClient:是接口,定义了发送 HTTP 请求、处理响应的基本行为。
- HttpGet:继承自- HttpRequestBase,用于创建 HTTP GET 请求,从服务器获取资源。
- HttpPost:继承自- HttpRequestBase,用于创建 HTTP POST 请求,向服务器提交数据。
- Closeable:是 Java 的一个接口,实现该接口的对象可调用- close()方法释放资源。
- CloseableHttpClient:实现了- HttpClient和- Closeable,是可关闭的 HTTP 客户端,可发送请求并处理响应。
示例
发送Get请求
            
            
              java
              
              
            
          
          /**
     * HttpClient发送Get请求
     * @throws IOException
     */
    @Test
    public void HttpClientTestGet() throws IOException {
        //创建HttpClients的对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //创建Http对象
        HttpGet httpGet = new HttpGet("http://localhost:8080/user/shop/status");
        //发送请求 接收响应结果
        CloseableHttpResponse response = httpClient.execute(httpGet);
        //获取浏览器状态码
        int httpcode=response.getStatusLine().getStatusCode();
        System.out.println("服务器状态码:"+httpcode);
        //获取响应体
        HttpEntity entity = response.getEntity();
        String s= EntityUtils.toString(entity);
        System.out.println(s);
        //关闭资源
        response.close();
        httpClient.close();
    }发送Post请求
            
            
              java
              
              
            
          
           /**
     * HttpClient发送Post请求
     */
    @Test
    public void HttpClientTestPost() throws JSONException, IOException {
        //创建HttpClient
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //创建Http对象
        HttpPost httpPost = new HttpPost("http://localhost:8080/admin/employee/login");
        //构造发送数据
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("username","admin");
        jsonObject.put("password","123456");
        StringEntity entity = new StringEntity(jsonObject.toString());
        //设置编码个数
        entity.setContentEncoding("utf-8");
        //数据格式
        entity.setContentType("application/json");
        //写入数据
        httpPost.setEntity(entity);
        //发送请求
        CloseableHttpResponse response = httpClient.execute(httpPost);
        //解析返回数据
        int statusCode=response.getStatusLine().getStatusCode();
        System.out.println("服务器状态码:"+statusCode);
        //获取响应体
        HttpEntity responseEntity = response.getEntity();
        String s=EntityUtils.toString(responseEntity);
        System.out.println(s);
    }