“unable to find valid certification path to requested target” when I post a https request to a URL

我的未来我决定 提交于 2019-12-25 08:22:56

问题


I have completed configuring SSL in my local Tomcat.
And the exception was thrown when I call 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();
    }

How can I fix the issue?

Thanks in advance!!


回答1:


Java requires a valid certification path to a known root CA. If you are trying to access a site with a self-signed certificate you will need to add the CA key for the self-signed cert to your keystore as a CA key. Assuming your CA certificate is in a file cacert.pem, use keytool as follows:

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


来源:https://stackoverflow.com/questions/9815989/unable-to-find-valid-certification-path-to-requested-target-when-i-post-a-http

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!