http2.0请求springboot接口

发布时间:2022-07-01 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了http2.0请求springboot接口脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

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下运行 还是有疑问。。。

脚本宝典总结

以上是脚本宝典为你收集整理的http2.0请求springboot接口全部内容,希望文章能够帮你解决http2.0请求springboot接口所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。