【问题标题】:How to call a https rest service without client certificate如何在没有客户端证书的情况下调用 https 休息服务
【发布时间】:2020-08-07 18:01:51
【问题描述】:

我将不得不在不发送证书的情况下使用 https 休息服务,但我的应用程序仍在尝试查找路径。

try
{
  HttpsURLConnection connection = (HttpsURLConnection) new URL(null, ResourceUtil.getEnvironmentMessage(URL), new sun.net.www.protocol.https.Handler()).openConnection();
  connection.setRequestMethod("POST");
  connection.setRequestProperty("Content-Type", "application/json");
  connection.connect();
  String inputRequest = buildJsonRequest(request);

  connection.setDoOutput(true);
  OutputStream ucdlRequestStream = connection.getOutputStream();
  requestStream.write(inputRequest.getBytes());
  requestStream.flush();

  if (connection.getResponseCode() != 200)
  {
    throw new SystemException("Failed : HTTP error code : " + connection.getResponseCode());
  }

  BufferedReader responseReader = new BufferedReader(new InputStreamReader((connection.getInputStream())));

  String output;
  LOGGER.info("Output from Server .... \n");
  while ((output = responseReader.readLine()) != null)
  {
    boolean hasActiveProducts = output.contains("ACTIVE");
    LOGGER.info(output);
  }
  connection.disconnect();
}
catch (IOException | EASystemException e)
{
  LOGGER.error(e.getMessage(), e);
  throw new SystemException(e.getMessage(), e);
}
LOGGER.info(String.valueOf(hasActiveProducts));
return hasActiveProducts;

看到以下错误: 原因:sun.security.validator.ValidatorException:PKIX 路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求目标的有效证书路径 在 sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:385)

【问题讨论】:

    标签: https rest-client


    【解决方案1】:

    通过大量的谷歌搜索,设法在下面找到。

    try
    {
      HttpsURLConnection connection = (HttpsURLConnection) new URL(null, url, new 
      sun.net.www.protocol.https.Handler()).openConnection();  
      connection.setRequestMethod("POST");
      connection.setRequestProperty("Content-Type", "application/json");
      String inputRequest = buildRequest(object);
      
      TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager()
      {
        public X509Certificate[] getAcceptedIssuers()
        {
          return null;
        }
    
        public void checkClientTrusted(X509Certificate[] x509Certificates, String s)
        {
        }
    
        public void checkServerTrusted(X509Certificate[] x509Certificates, String s)
        {
        }
      }};
    
      // Install the all-trusting trust manager
      SSLContext sc = SSLContext.getInstance("SSL");
      sc.init(null, trustAllCerts, null);
      connection.setDefaultSSLSocketFactory(sc.getSocketFactory());
      
      connection.setDoOutput(true);
      connection.setDoInput(true);
      OutputStream ucdlRequestStream = connection.getOutputStream();
      ucdlRequestStream.write(inputRequest.getBytes());
      ucdlRequestStream.flush();
      ucdlRequestStream.close();
      LOGGER.info(ucdlRequestStream.toString());
      LOGGER.info(connection.getOutputStream().toString());
      LOGGER.info(String.valueOf(connection.getResponseCode()));
    
      if (connection.getResponseCode() != 200)
      {
        throw new EASystemException("Failed : HTTP error code : " + connection.getResponseCode());
      }
     }
      catch (IOException | EASystemException e)
      {
        LOGGER.error(e.getMessage(), e);
        throw new SystemException(e.getMessage(), e);
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-19
      • 1970-01-01
      相关资源
      最近更新 更多