【问题标题】:"unable to find valid certification path to requested target" when I post a https request to a URL当我向 URL 发布 https 请求时,“无法找到请求目标的有效证书路径”
【发布时间】:2012-04-06 15:25:52
【问题描述】:

我已在本地 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 exception post https


    【解决方案1】:

    Java 需要到已知根 CA 的有效证书路径。如果您尝试使用自签名证书访问站点,则需要将自签名证书的 CA 密钥作为 CA 密钥添加到您的密钥库中。假设您的 CA 证书在文件 cacert.pem 中,请使用 keytool,如下所示:

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

    【讨论】:

    • 我在哪里可以获得“cacert.pem”?
    • 这是我在 server.xml 中的配置:
    • 在回答“我在哪里可以获得 cacert.pem”之前,您必须知道远程网站的 SSL 证书是如何签名的。是商业证明吗?自签?其他?
    猜你喜欢
    • 2020-11-11
    • 2011-10-18
    • 2020-09-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多