当我向REST服务器发出POST请求时,出现以下异常: PKIX路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求的目标异常的有效证书路径。我想创建一个不验证证书链并且不确定如何在这种情况下实现此目标的信任管理器。
在HttpPostReq中,我有:
public class HttpPostReq {
HttpPost createConnectivity(String restUrl, String username, String password)
{
HttpPost post = new HttpPost(restUrl);
String auth=new StringBuffer(username).append(":").append(password).toString();
byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("US-ASCII")));
String authHeader = "Basic " + new String(encodedAuth);
post.setHeader("AUTHORIZATION", authHeader);
post.setHeader("Content-Type", "application/json");
post.setHeader("Fintech-Platform-TenantId", "default");
return post;
}
void executeHttpRequest(String jsonData, HttpPost httpPost) throws UnsupportedEncodingException, IOException
{
HttpResponse response=null;
String line = "";
StringBuffer result = new StringBuffer();
httpPost.setEntity(new StringEntity(jsonData));
HttpClient client = HttpClientBuilder.create().build();
response = client.execute(httpPost);
System.out.println("Post parameters : " + jsonData );
System.out.println("Response Code : " +response.getStatusLine().getStatusCode());
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
while ((line = reader.readLine()) != null){ result.append(line); }
System.out.println(result.toString());
}
void executeReq(String jsonData, HttpPost httpPost)
{
try{
executeHttpRequest(jsonData, httpPost);
}
catch (UnsupportedEncodingException e){
System.out.println("error while encoding api url : "+e);
}
catch (IOException e){
System.out.println("ioException occured while sending http request : "+e);
}
catch(Exception e){
System.out.println("exception occured while sending http request : "+e);
}
finally{
httpPost.releaseConnection();
}
}
}
在HttpClientBridge中,我有:
public static void main(String[] args) throws JSONException {
String restUrl="https://localhost:8449/wesecbs/api/v1/loans";
String username="myname";
String password="mypassword";
loan.put("interestType", "0");
loan.put("locale", "en_GB");
String jsonData=loann.toString();
HttpPostReq httpPostReq=new HttpPostReq();
HttpPost httpPost=httpPostReq.createConnectivity(restUrl , username, password);
httpPostReq.executeReq( jsonData, httpPost);
}