java - 获取方法抛出“javax.net.ssl.SSLException”异常

我正在尝试点击 api1,它为我提供了一个用于 api2 身份验证的令牌。但是,我从 Connection.getOutputStream() 方法中收到此错误,引发了“javax.net.ssl.SSLException”异常。知道如何解决这个问题吗?

public HttpsURLConnection getHttpsURLConnection(HttpParameterSetter parameters, URL url, String method) throws IOException {
        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
        connection.setRequestMethod(method);
        parameters.getHeaders().forEach(connection::setRequestProperty);
        return connection;
    }    


public String requests(URL url) throws Exception {
    url = new URL(url.toString() + "?" + parameters.toString());
    HttpsURLConnection connection = getHttpsURLConnection(this.parameters, url, this.method);
    connection.setDoOutput(true);
    BufferedReader in = null;
    DataOutputStream wr = null;
    try {
                if (!payload.equals("")) {
                    wr = new DataOutputStream(connection.getOutputStream());
                    wr.writeBytes(this.getPayload());
                    wr.flush();
                    wr.close();
                }
                in = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
                StringBuilder inputLine = new StringBuilder();
                String line;
                while ((line = in.readLine()) != null)
                    inputLine.append(line);
                return inputLine.toString();
            } catch (Exception e) {
                log.error("Exception occurred during token call "+e.getMessage());
                throw e;
            } finally {
                try{
                    if (wr != null)
                        wr.close();
                }catch (Exception e){
                    log.error("Error occurred while closing DataOutputStream in the token call: "+e.getMessage());
                }
                try{
                    if (in != null)
                        in.close();
                }catch (Exception e){
                    log.error("Error occurred while closing BufferedReader in the token call: "+e.getMessage());
                }
            }
}**strong text**

回答1

您尝试连接的服务器是否使用自签名证书?如果是这样,请确保您已将自签名证书导入您的 jre KeyStore。

这是一个例子:

keytool -keystore "%JAVA_HOME%\jre\lib\security\cacerts" -importcert -alias <alias> -file <certificate> -trustcacerts -keypass changeit -storepass changeit

<certificate> 是证书的文件名,例如 xxx.cerxxx.crt

相似文章

java - 如何在不关闭另一个的情况下关闭 JFrame

我知道这个问题已被多次解决,但我尽一切努力使其正常工作。首先,当用户登录合法时,我创建了一个JDialog作为加载弹出窗口。然后我创建一个Timer来自动关闭框架,然后我的主菜单就会出现。但是,我的主...

最新文章