我正在尝试在restful webservice中发送HTTPS请求并获取javax.net.ssl.SSLHandshakeException

时间:2017-05-02 11:54:15

标签: java ssl

下面的代码适用于HTTP请求,但不适用于HTTPS。我收到了这个错误:

  

javax.net.ssl.SSLHandshakeException:   sun.security.validator.ValidatorException:PKIX路径构建失败:   sun.security.provider.certpath.SunCertPathBuilderException:无法   找到所请求目标的有效证书路径

当我通过hurl.it发送请求时,使用HTTPS的网址正常。是否有https请求的客户端构建器?

这是我的代码:

HttpClient httpClient = null;
        HttpRequestBase baseRequest = null;
        String apiResponse = null;
        try {
            httpClient = getClientConnection(url, clientConfiguration.isProxyEnabled());
            baseRequest = getHttpClient(url, clientConfiguration.getRequestMethod());

            if (headers != null) {
                for (Entry<String, String> entry : headers.entrySet()) {
                    baseRequest.addHeader(entry.getKey(), entry.getValue());
                }
            }
            if (!clientConfiguration.getRequestMethod().equals(RequestMethod.GET)) {
                // checking for post body parameters
                if (clientConfiguration.getURLParameters() != null
                        && !clientConfiguration.getURLParameters().isEmpty()) {
                    List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
                    for (Entry<String, String> param : clientConfiguration.getURLParameters().entrySet()) {
                        postParameters.add(new BasicNameValuePair(param.getKey(), param.getValue()));
                    }
                    if (baseRequest instanceof HttpPost) {
                        ((HttpPost) baseRequest).setEntity(new UrlEncodedFormEntity(postParameters));
                    } else if (baseRequest instanceof HttpPut) {
                        ((HttpPut) baseRequest).setEntity(new UrlEncodedFormEntity(postParameters));
                    } else if (baseRequest instanceof CoreDelete) {
                        ((CoreDelete) baseRequest).setEntity(new UrlEncodedFormEntity(postParameters));
                    }
                }
            }
            System.out.println("URL " + url);
            if (clientConfiguration.getRequestBody() != null) {
                HttpEntity entity = new ByteArrayEntity(clientConfiguration.getRequestBody().getBytes("UTF-8"));
                if (baseRequest instanceof HttpPost) {
                    ((HttpPost) baseRequest).setEntity(entity);
                } else if (baseRequest instanceof HttpPut) {
                    ((HttpPut) baseRequest).setEntity(entity);
                } else if (baseRequest instanceof CoreDelete) {
                    ((CoreDelete) baseRequest).setEntity(entity);
                }
            }
            HttpResponse response = httpClient.execute(baseRequest);

0 个答案:

没有答案