zoukankan      html  css  js  c++  java
  • 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下运行 还是有疑问。。。

  • 相关阅读:
    SQL中如何用一个表的列更新另一个表的列?
    ASPxGridView利用CheckBox实现全选
    DevExpress.NETv8.1(Web Controls)学习笔记
    ALSA vs OSS
    video telephone
    uClinux系统分析 转
    Using KVM On Ubuntu 7.10 (Gutsy Gibbon)转
    想买开发板,我真的需要么?
    uClinux的内存管理转
    各种开源软件授权方式的选择 (zt)
  • 原文地址:https://www.cnblogs.com/HappyKnockOnCode/p/15486609.html
Copyright © 2011-2022 走看看