java 模拟HTTP2.0请求

您所在的位置:网站首页 java模拟http java 模拟HTTP2.0请求

java 模拟HTTP2.0请求

2023-12-20 07:53| 来源: 网络整理| 查看: 265

关于HTTP/1.0 HTTP/1.1 HTTP/2.0    https://www.cnblogs.com/heluan/p/8620312.html

目前主流网站如某宝某东都已经支持,下面将用java程序模拟发起HTTP/2.0 GET请求。

要求:

Jdk 1.9+编译: jdk1.9工具:idea

第一步 创建项目

第二步 source目录下创建module-info.java

module demo1 { requires jdk.incubator.httpclient; }

第三步 写代码

package com.example.demo1; import jdk.incubator.http.HttpClient; import jdk.incubator.http.HttpRequest; import jdk.incubator.http.HttpResponse; import java.io.IOException; import java.net.URI; public class HttpClientUsg { public String requestByGetMethod(String url) throws IOException, InterruptedException { HttpClient httpClient = HttpClient.newBuilder().build(); HttpRequest request = HttpRequest.newBuilder(URI.create(url)) // .header("Content-Type", "application/json") .version(HttpClient.Version.HTTP_2) .GET() .build(); HttpResponse httpResponse = httpClient.send(request, HttpResponse.BodyHandler.asString()); System.out.println(httpResponse.toString()); System.out.println(httpResponse.headers().toString()); System.out.println(httpResponse.body().toString()); return url; } public static void main(String args[]) { HttpClientUsg httpClientUtil = new HttpClientUsg(); String url = "https://www.baidu.com"; String res = null; try { res = httpClientUtil.requestByGetMethod(url).split("/n")[0]; System.out.println(res); } catch (Exception e) { e.printStackTrace(); } } }

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3