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

     

  • 相关阅读:
    Java集合类ArrayList的应用-杨辉三角的前n行
    js数据类型和判断数据类型的方法
    html父级标签和子标签的点击事件重叠问题
    【泛函分析】距离空间的完备性
    python+vue+elementui实验室课程在线答疑系统php_java
    simucpp系列教程(7)坟墓
    帆软 列表自动滚动脚本
    PyTorch(四)Torchvision 与 Transforms
    第15讲:DCL类型的SQL语句之用户权限控制
    torch lighting 设置多个优化器
  • 原文地址:https://blog.csdn.net/ONEBEYONDFANS/article/details/133164736