"unable to find valid certification path to requested target"当我向 URL 发布 https 请求时



我已经在本地Tomcat中完成了SSL的配置
当我调用getOutputStream()时抛出了异常

public static InputStream send( String uri, Map<String, String> queryString, 
            Map<String, String> headers, String method, String reqBody) throws IOException
    {
        String body = (reqBody != null ? reqBody : "");
        //URL myURL = new URL(addUrlParam(uri, queryString));
        URL myURL = new URL(uri);
        HttpURLConnection httpConn = (HttpURLConnection)myURL.openConnection();
        httpConn.setRequestMethod(method);
        httpConn.setRequestProperty("Content-Length", String.valueOf(body.toString().getBytes().length));
        if ( headers != null ) {
            for ( String key : headers.keySet() ) {
                httpConn.setRequestProperty(key, headers.get(key));
            }
        }
        httpConn.setDoInput(true);
        //POST
        if (!HTTP_GET.equals(method) || body.length() > 0) {
            httpConn.setDoOutput(true);
            httpConn.setUseCaches(false); //POST do not use user caches
            ***httpConn.getOutputStream().write(body.toString().getBytes());***
            httpConn.getOutputStream().flush();
        }
        return httpConn.getInputStream();
    }

如何解决此问题?

提前感谢!!

Java需要一个到已知根CA的有效证书路径。如果您试图使用自签名证书访问网站,则需要将自签名证书的CA密钥作为CA密钥添加到您的密钥库中。假设您的CA证书位于文件cacert.pem中,请按如下方式使用keytool

keytool -importcert -file cacert.pem -keystore client.jks -storepass some-password

最新更新