我正在尝试将 Https Post 请求发送到使用自签名证书的服务器,并且我收到一个带有错误的异常:peer not authenticated
.
我用谷歌搜索,发现问题的原因是服务器使用的是自签名证书。我怎样才能抑制这个错误?
我正在使用以下函数发送发布请求:
public String sendPost(final String request, final String postData) throws ClientProtocolException, IOException {
String result = null;
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(request);
ByteArrayEntity postDataEntity = new ByteArrayEntity(postData.getBytes());
httpPost.setEntity(postDataEntity);
CloseableHttpResponse response = httpclient.execute(httpPost);
try {
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity);
EntityUtils.consume(entity);
} finally {
response.close();
}
return result;
}
我错过了什么来抑制这个错误?我不想尝试捕捉这个异常。我想正确配置它,以便接受自签名证书。我正在使用 Httpclient 4.1。
谢谢你!