HttpClient
本文最后更新于98 天前,其中的信息可能已经过时,如有错误请发送邮件到z18096561915@outlook.com

介绍

HTTP客户端是APACHE Jakarta Common下的子项目,可以用来提供高效的、最新的、功能丰富的支持http协议
的客户端编程工具包,并且它支持Http协议最新的版本和建议。

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>

核心AP1:

  • HttpClient
  • HttpClients
  • CloseableHttpClient
  • HttpGet
  • HttpPost

发送请求步骤

  • 创建HttpClient对象
  • 创建Http请求对象
  • 调用HttpClient的execute方法发送请求

入门案例

导入坐标

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.13</version>
</dependency>

创建测试类 HttpClientTest

@SpringBootTest
public class HttpClientTest {
        
}

创建测试通过httpclient发送GET请求

  • // 1.创建HttpClient对象
    CloseableHttpClient httpClient = HttpClients.createDefault();
  • // 2.创建HttpGet对象,设置请求地址
    HttpGet httpGet = new HttpGet("http://localhost:8080/user/shop/status");
  • // 3.使用HttpClient对象发送请求,并获取响应
    CloseableHttpResponse response = httpClient.execute(httpGet);
  • // 4.打印响应状态码
    System.out.println("状态码为:" + response.getStatusLine().getStatusCode());
    HttpEntity entity = response.getEntity();
    String boby = EntityUtils.toString(entity);
    // 打印响应内容
    System.out.println("响应内容为:" + boby);
  • // 最后关闭HttpClient和HttpResponse对象
    response.close();
    httpClient.close();

完整代码

/**
     * 测试通过httpclient发送GET请求
     */
    @Test
    public void testGet() throws Exception {
        // 创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();

        // 创建HttpGet对象,设置请求地址
        HttpGet httpGet = new HttpGet("http://localhost:8080/user/shop/status");
        // 使用HttpClient对象发送请求,并获取响应
        CloseableHttpResponse response = httpClient.execute(httpGet);
        // 打印响应状态码
        System.out.println("状态码为:" + response.getStatusLine().getStatusCode());
        HttpEntity entity = response.getEntity();
        String boby = EntityUtils.toString(entity);
        // 打印响应内容
        System.out.println("响应内容为:" + boby);

        // 关闭HttpClient和HttpResponse对象
        response.close();
        httpClient.close();
    }

运行测试结果

创建测试通过httpclient发送POST请求

  • // 1.创建HttpClient对象
    CloseableHttpClient httpClient = HttpClients.createDefault();
  • // 2.创建HttpPost对象,设置请求地址和请求参数
    HttpPost httpPOST = new HttpPost("http://localhost:8080/admin/employee/login");
  • // 3.设置请求参数JSON
    JSONObject json = new JSONObject();
    json.put("username", "admin");
    json.put("password", "123456");
    StringEntity entity = new StringEntity(json.toString());
  • //4.指定请求内容类型为json
    entity.setContentType("application/json");
  • //5.指定请求参数编码格式
    entity.setContentEncoding("UTF-8");
  • //6.设置请求参数
    httpPOST.setEntity(entity);
  • // 7.使用HttpClient对象发送请求,并获取响应
    CloseableHttpResponse response = httpClient.execute(httpPOST);
  • // 8.打印响应状态码
    System.out.println("状态码为:" + response.getStatusLine().getStatusCode());
    HttpEntity responseEntity = response.getEntity();
    String responseBody = EntityUtils.toString(responseEntity);
  • // 9.打印响应内容
    System.out.println("响应内容为:" + responseBody);
  • // 10.关闭HttpClient和HttpResponse对象
    response.close();
    httpClient.close();

完整代码

/**
 * 测试通过httpclient发送POST请求
 */
@Test
public void testPOST() throws Exception {
    // 1.创建HttpClient对象
    CloseableHttpClient httpClient = HttpClients.createDefault();
    // 2.创建HttpPost对象,设置请求地址和请求参数
    HttpPost httpPOST = new HttpPost("http://localhost:8080/admin/employee/login");
    // 3.设置请求参数
    JSONObject json = new JSONObject();
    json.put("username", "admin");
    json.put("password", "123456");
    StringEntity entity = new StringEntity(json.toString());
    //4.指定请求内容类型为json
    entity.setContentType("application/json");
    //5.指定请求参数编码格式
    entity.setContentEncoding("UTF-8");
    //6.设置请求参数
    httpPOST.setEntity(entity);
    // 7.使用HttpClient对象发送请求,并获取响应
    CloseableHttpResponse response = httpClient.execute(httpPOST);
    // 8.打印响应状态码
    System.out.println("状态码为:" + response.getStatusLine().getStatusCode());
    HttpEntity responseEntity = response.getEntity();
    String responseBody = EntityUtils.toString(responseEntity);
    // 9.打印响应内容
    System.out.println("响应内容为:" + responseBody);
    // 10.关闭HttpClient和HttpResponse对象
    response.close();
    httpClient.close();

}

运行测试结果

觉得有帮助可以投喂下博主哦~感谢!
作者:JoyBoy
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0协议
转载请注明文章地址及作者哦~
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇