I have the following java code to send a POST request.
When executed it is returning the following error:
PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
From what I've studied about the topic the reason is usually because the url of the POST request is https and its certificate needs to be added to the jdk keystore (cacerts).
So I downloaded the certificate from the https url and added to the cacerts as follows.
keytool -import -alias testcert1 -keystore "...\jdk1.7.0_80\jre\lib\security\cacerts" -file certificate.cer
run at \jdk1.7.0_80\jre\lib\security\cacerts
The certificate is successfully imported to cacerts.
Running the command to read cacerts the certificate is displayed.
But the error still continues.
Any other ideas on what could be the issue?
Could the cacerts being consulted by the app be in some other place?
If that's the case how to know where.
import javax.net.ssl.SSLContext;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.client.methods.HttpPost;
import com.google.gson.Gson;
import org.apache.http.entity.StringEntity;
import org.apache.http.HttpResponse;
public void sendPostRequest(Object src)
{
SSLContext context = SSLContexts.custom().useProtocol("TLSv1.2").build();
HttpClient client = HttpClientBuilder.create().setSSLContext(context).build();
HttpPost httpPost = new HttpPost(httpsurl);
httpPost.setHeader("User-Agent", "abc");
httpPost.setHeader("Content-Type","application/json; charset=UTF-8");
httpPost.setHeader("OtherInfo", "xyz");
httpPost.setHeader("Accept", "application/json");
Gson gson = new Gson();
StringEntity input;
input = new StringEntity(gson.toJson(src));
input.setContentType("application/json");
httpPost.setEntity(input);
HttpResponse httpResponse = client.execute(httpPost);
}