• 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. }

     

  • 相关阅读:
    震惊 !!!DOM还能这么用,让我们跟随小编一起去看看吧 !
    啸叫检测的方法:基于DSP的实现
    Unity Editor 遍历指定文件夹下的所有prefab
    非连续分配管理方式之基本分页存储管理
    golang 工程组件 grpc-gateway—yaml定义http规则,和自定义实现网关路由
    Python3+selenium3
    yolov5 create_dataloader原码及解析
    效率为王,居家办公必备的5款小工具
    利用改进的YOLOv5模型对玉米和杂草进行精准检测和精准喷洒
    Spring Boot 程序优化的 14 个小妙招!
  • 原文地址:https://blog.csdn.net/ONEBEYONDFANS/article/details/133164736