当我向网址发布https请求时,“无法找到所请求目标的有效证书路径”

时间:2012-03-22 03:22:26

标签: java exception post 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();
    }

如何解决问题?

提前致谢!!

1 个答案:

答案 0 :(得分:1)

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

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