暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

jdk11新特性HttpClient

785
  • HttpClient可以向指定的域名和端口发送http请求【可以是get请求,post请求,异步get请求,异步post请求】

  • 思路过程

    • 构建请求接口的URI

    • 创建HttpClient的对象【相当于客户端】

    • 创建HttpRequest的对象

      • 默认是get请求对象,可以设置header信息,也可以设置params信息

      • 可以通过POST()指定这个请求是post请求

        • post提交方式有两种

          • json的格式(Content-Type=application/json);

          • form表单的格式(Content-Type=application/x-www-form-urlencoded)

    • 发送http请求并获得响应结果【发送方式可以是同步发送也可以是异步发送】

  • 注意事项:

    • HttpClient默认是使用http1.1版本的,如果我们访问的URI接口是基于http2协议的,那么默认的HttpClient是不支持访问的!! 需要显示指明我们创建的HttpClient是http2版本的,才能访问基于http2协议开发的接口。

    var httpClient = HttpClient.newBuilder()
    .connectTimeout(Duration.ofMillis(3000))
    //显示指明我们使用的是Http2的协议,默认是http1.1
    .version(HttpClient.Version.HTTP_2)
    .build();
    • HttpClient的接口介绍

      • HttpClient.Builder 【HttpClient 构建⼯具类】

      • HttpRequest.Builder 【HttpRequest 构建⼯具类】

      • HttpRequest.BodyPublisher 【将java 对象转换为可发送的HTTP request body字节流, 如form表单提交】

      • HttpResponse.BodyHandler 【处理接收到的 Response Body】

      public class Main {
      private static final String targetUrl1 = "http://api.xdclass.net/pub/api/v1/web/web_login";
      private static final String targetUrl2 = "http://api.xdclass.net/pub/api/v1/web/all_category";

      public static void main(String[] args) throws Exception {
      testGet();
      testPost();
      }

      private static final void testPost() {
      //构造URI 访问接口
      final URI uri = URI.create(targetUrl1);
      //创建httpclient并设置超时时间
      var httpClient = HttpClient.newBuilder().connectTimeout(Duration.ofMillis(5000)).build();

      //构建post请求报文
      var request = HttpRequest.newBuilder().uri(uri)
      //json格式则下面数据
      //.header("Content-Type","application/json")
      //.POST(HttpRequest.BodyPublishers.ofString("{\"phone\":\"13113777338\",\"pwd\":\"1234567890\"}"));

      //form表单则使用下面的配置
      .header("Content-Type","application/x-www-form-urlencoded")
      .POST(HttpRequest.BodyPublishers.ofString("phone=13113777338&pwd=1234567890")).build();

      try {
      //发送post请求并获取返回结果
      var response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
      System.out.println(response.body());
      } catch (Exception e) {
      e.printStackTrace();
      }
      }

      private static final void testGet() {
      //构造URI 访问接口
      final URI uri = URI.create(targetUrl2);
      //httpclient客户端
      HttpClient httpClient = HttpClient.newBuilder()
      //设置超时实际
      .connectTimeout(Duration.ofMillis(5000))
      .build();


      //构建请求报文,默认是get请求的报文
      HttpRequest request = HttpRequest.newBuilder().timeout(Duration.ofMillis(3000))
      .header("key1", "v1")
      .header("key2", "v2")
      .uri(uri).build();
      try {
      //发起get请求并获取返回的response
      HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
      System.out.println(response.body());
      } catch (Exception e) {
      e.printStackTrace();
      }
      }

      }


      文章转载自梁霖编程工具库,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

      评论