小白求助,采用的是Https请求,请求大牛介绍下此错误到底是什么原因引起,百度多次无果,无奈只能请求各位大佬。
以下是代码以及报错截图:
@SuppressWarnings("unchecked")
public Map invokeVoiceCheckMethod(Map params) {
Map datas = (Map) params.get("data");
QltyChkCallLine voiceCheckData = new QltyChkCallLine();
String a = String.valueOf(datas.get("wtfId"));
voiceCheckData.setFileId(String.valueOf(datas.get("wtfId")));
voiceCheckData.setFileCodec(String.valueOf(datas.get("wtfVoicePath")));
voiceCheckData.setFilePath(String.valueOf(datas.get("wtfVoicePath")));
try {
Map data = transferVoiceData(voiceCheckData);
Map request = new HashMap();
request.put("cId", sceneCode);
List col = new ArrayList<String>();
col.add(data);
request.put("callList", col);
String jsonStr = JSONObject.fromObject(request).toString();
Map map = new HashMap();
map.put("data",jsonStr);
    String path="src/main/resources/localhost.p12";
//     String retJson= callRemoteService(url, jsonStr);
String retJson = send("https://10.xx.xx.xx:8057/imchat/aichat/xxxxxx", map,"UTF-8",new FileInputStream(new File(path)),"IMChat@2018");
//     String retJson = doPost(url, jsonStr,"UTF-8",new FileInputStream(new File(path)),"IMChat@2018");
insertResultToDataBase(retJson, data);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static String send(String url, Map<String, String> map, String encoding, InputStream certStream, String pwd) throws IOException {
String body = "";
SSLContext sslcontext = null;
try {
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(certStream, pwd.toCharArray());
// 相信自己的CA和所有自签名的证书
sslcontext = SSLContexts.custom().loadTrustMaterial(trustStore, new TrustSelfSignedStrategy()).build();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
//证书io流关闭
certStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}

// 设置协议http和https对应的处理socket链接工厂的对象
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.INSTANCE).register("https", new SSLConnectionSocketFactory(sslcontext)).build();

PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
HttpClients.custom().setConnectionManager(connManager);

//创建自定义的httpclient对象
CloseableHttpClient client = HttpClients.custom().setConnectionManager(connManager).build();

//创建post方式请求对象
HttpPost httpPost = new HttpPost(url);

//装填参数
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
if (map != null) {
for (Map.Entry<String, String> entry : map.entrySet()) {
nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
}
//设置参数到请求对象中
httpPost.setEntity(new UrlEncodedFormEntity(nvps, encoding));

System.out.println("请求地址:" + url);
// System.out.println("请求参数:" + nvps.toString());

//设置header信息
//指定报文头【Content-type】、【User-Agent】
httpPost.setHeader("Accept", "application/json");
// httpPost.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");

//执行请求操作,并拿到结果(同步阻塞)
CloseableHttpResponse response = null;
try {
response = client.execute(httpPost);

//获取结果实体
HttpEntity entity = response.getEntity();
if (entity != null) {
//按指定编码转换结果实体为String类型
body = EntityUtils.toString(entity, encoding);
}
EntityUtils.consume(entity);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
//释放链接
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return body;
}

解决方案 »

  1.   

    问题分析:颁发的证书是给制定域名的而非IP地址,因为服务器没有加入Public DNS,所以域名无法访问,我在调用接口时直接使用了IP地址,而不是域名。问题解决:在hosts配置文件中添加IP地址和域名的映射记录,在调用接口时使用域名进行调用,发现问题解决。https://www.cnblogs.com/xusweeter/p/7659953.html
      

  2.   


    这个方法我试过了,修改hosts文件利用域名还是报一样的错误。