• HttpClient基本使用


    一、初识HttpClient

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

    用来发送http请求或者解析http响应。

    官网地址:http://hc.apache.org/index.html

    特点:

    • 基于标准、纯净的Java语言。实现了Http1.0和Http1.1

    • 以可扩展的面向对象的结构实现了Http全部的方法(GET, POST, PUT, DELETE, HEAD, OPTIONS, and TRACE)

    • 支持HTTPS协议。(https=http+ssl)

    • 通过Http代理建立透明的连接。

    • 自动处理Set-Cookie中的Cookie。

    二、HttpClient请求

            在使用之前,需要导包,使用maven:

    1. <dependency>
    2. <groupId>org.apache.httpcomponents.client5groupId>
    3. <artifactId>httpclient5artifactId>
    4. <version>5.2.1version>
    5. dependency>
    测试代码如下:
    1. package com.leyou.httpdemo;
    2. import org.apache.http.client.methods.HttpGet;
    3. import org.apache.http.client.methods.HttpPost;
    4. import org.apache.http.impl.client.BasicResponseHandler;
    5. import org.apache.http.impl.client.CloseableHttpClient;
    6. import org.apache.http.impl.client.HttpClients;
    7. import org.junit.Before;
    8. import org.junit.Test;
    9. import java.io.IOException;
    10. public class HTTPTest {
    11. CloseableHttpClient httpClient; //声明HttpClient
    12. /**
    13. * 执行请求之前先初始化 创建HttpClient实例
    14. */
    15. @Before
    16. public void init() {
    17. httpClient = HttpClients.createDefault();
    18. }
    19. @Test
    20. public void testGet() throws IOException {
    21. HttpGet request = new HttpGet("http://www.baidu.com");
    22. String response = this.httpClient.execute(request, new BasicResponseHandler());
    23. System.out.println("=======================================================");
    24. System.out.println(response);
    25. System.out.println("=======================================================");
    26. }
    27. @Test
    28. public void testPost() throws IOException {
    29. HttpPost request = new HttpPost("https://www.oschina.net/");
    30. request.setHeader("User-Agent",
    31. "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36");
    32. String response = this.httpClient.execute(request, new BasicResponseHandler());
    33. System.out.println("=======================================================");
    34. System.out.println(response);
    35. System.out.println("=======================================================");
    36. }
    37. @Test
    38. public void testGetPojo() throws IOException {
    39. HttpGet request = new HttpGet("http://localhost:8080/hello");
    40. String response = this.httpClient.execute(request, new BasicResponseHandler());
    41. System.out.println("=======================================================");
    42. System.out.println(response);
    43. System.out.println("=======================================================");
    44. }
    45. }

     

  • 相关阅读:
    读Bilgin Ibryam 新作 《Dapr 是一种10倍数 平台》
    信息系统项目管理师Part17-云计算
    微信登录的接口
    2023 Google 开发者大会
    [CG从零开始] 3. 安装 pyassimp 库加载模型文件
    d435i 相机和imu标定
    改进粒子滤波的无人机三维航迹预测方法(基于Matlab代码实现)
    【设计模式】装饰器模式(结构型)⭐⭐
    单节点高并发Linux服务器影响接入能力的因素有哪些
    208. 开关问题 - 异或方程组
  • 原文地址:https://blog.csdn.net/ONEBEYONDFANS/article/details/133164736