http2.0请求springboot接口

本文讲述了在Java中使用HttpClient进行HTTP2.0请求SpringBoot接口时遇到的问题,包括Controller接口显示协议版本为HTTP1.1,以及HTTP2.0是否必须在HTTPS下运行的疑惑。通过模拟请求、修改代码和使用curl命令,最终成功发送HTTP2.0请求并确认协议版本。同时,文章探讨了HTTPS的安全性以及HTTP2.0与HTTPS的关系。
摘要由CSDN通过智能技术生成

http2.0请求springboot接口

参考博客:https://blog.csdn.net/sinat_33189520/article/details/103716544

问题背景:项目中的某个Controller接口是否支持http2.0请求

#、使用java模拟下发http2.0请求

环境:jdk11+;我的是jdk17;其实参考资料使用的是jdk9,这里改动了一些类。

撸代码:

客户端请求方

import java.io.IOException;
import java.net.URI;
import javax.net.ssl.*;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

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)//在这里尝试http2.0和http1.1,验证请求
//                .version(HttpClient.Version.HTTP_1_1)
//                .GET()
                .POST(HttpRequest.BodyPublishers.ofString(""))//不同的请求方式
                .build();
        trustEveryone();
        HttpResponse httpResponse = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
        request.bodyPublisher();
        System.out.println(httpResponse.version());
        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();
        //关于url使用http还是https后面讨论
        String url = "http://localhost:31105/callback";
//        String url = "http://localhost:8083/hello";
//        String url = "http://www.cnblogs.com/xifenglou/p/9394948.html"
//        String url = "https://localhost:8083/hello";
        String res = null;
        try {
            res = httpClientUtil.requestByGetMethod(url).split("/n")[0];
            System.out.println(res);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void trustEveryone() {

        try {
            HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            });

            SSLContext context = SSLContext.getInstance("TLS");
            context.init(null, new X509TrustManager[]{new X509TrustManager() {
                public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                }

                public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                }

                public X509Certificate[] getAcceptedIssuers() {
                    return new X509Certificate[0];
                }
            }}, new SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

请求接口

@RequestMapping("/callback")
public void Controller(HttpServletRequset request){
    log.info("协议版本号:"+request.getProtocol);
}

  1. 使用http1.1请求 URLhttp://localhost:31105/callback 可以通,证明请求方没有问题,Controller接口接收请求也正常。

  2. 使用http2.0请求URL可以通,但是在Controller接口打印 协议版本号为http1.1,并且请求方返回对象打印版本号http1.1

  3. **疑问:**在查的资料用有写 HTTP2.0只允许在HTTPS下运行,所以需要您开启HTTPS方可启用。据我理解https是http+SSL,在传输数据上更安全。直到最后也不知道这句话是否正确,如果知道的同志可评论下。

  4. 于是中间走了许多弯路,,尝试了Springboot同时支持http和https、自定义证书……后来发现上面的请求程序无法请求,因为证书是我自定义的,所以还要忽略证书,但是代码没有写出来,,实在是学习能力太差。

  5. 最后参考博客:https://blog.csdn.net/sinat_33189520/article/details/103716544,使用方法二

  6. 接口打印了协议版本号为http1.0 ,但是返回对象打印了 http2.0 ;

  7. 换一种请求方式:在linux系统使用curl(因为在windows系统中是在不知道怎么安装curl的http2.0协议),并且发起2.0请求。Controller接口打印协议版本号:http2.0

    curl  --http2-prior-knowledge --location --request POST  http://xx.xxx.xxx.xx:8080/callback
    

    到这里我就认为可以支持http2.0协议了,但是对于http2.0只允许在https下运行 还是有疑问。。。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值