丰常青 2016-01-08 01:07 采纳率: 0%
浏览 2452

Java发送https请求报错

    KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
            FileInputStream instream = new FileInputStream(new File(KEYSTORE_FILE_PATH));
    //
    //          try {
        //  trustStore.load(instream, KEYSTORE_PASSWORD.toCharArray());
    //          } finally {
    //              instream.close();
    //          }

            // Trust own CA and all self-signed certs
            //SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(trustStore, new TrustSelfSignedStrategy()).build();
     //         SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build();
            // Allow TLSv1 protocol only
     //         SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext,
    //                          new String[] { "TLSv1" }, null,
    //                          SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
    //          httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
    //之前用过证书,后来证书有问题就没有用了,用的是任意url都信任的方式,如下:


     SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
                 public boolean isTrusted(X509Certificate[] chain,
                                 String authType) throws CertificateException {
                     return true;
                 }
             }).build();

             SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
             CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
                        HttpPost httpPost = new HttpPost("https://localhost:8080/login");
                        httpClient.excute(httpPost);

报错如下:javax.net.ssl.SSLException: hostname in certificate didn't match: !=

  • 写回答

2条回答

  • oyljerry 2016-01-08 01:47
    关注

    hostname in certificate didn't match

    所用的证书不是这个host对应的,验证失败

    评论

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面