栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

http2.0请求springboot接口

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/362274.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号