本人小白,请教大牛;采用的是HTTPSPost请求,使用Postman工具测试过能链接,采用的是IP直连方式。请各位大牛大哥解答一下这个问题出现的情况是什么原因引起?
以下是代码以及错误截图
publicstaticStringsend(Stringurl,Mapmap,Stringencoding,InputStreamcertStream,Stringpwd)throwsIOException{
Stringbody="";
SSLContextsslcontext=null;
try{
KeyStoretrustStore=KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(certStream,pwd.toCharArray());
//相信自己的CA和所有自签名的证书
sslcontext=SSLContexts.custom().loadTrustMaterial(trustStore,newTrustSelfSignedStrategy()).build();
}catch(Exceptione){
e.printStackTrace();
}finally{
try{
//证书io流关闭
certStream.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
//设置协议http和https对应的处理socket链接工厂的对象
RegistrysocketFactoryRegistry=RegistryBuilder.create().register("http",PlainConnectionSocketFactory.INSTANCE).register("https",newSSLConnectionSocketFactory(sslcontext)).build();
PoolingHttpClientConnectionManagerconnManager=newPoolingHttpClientConnectionManager(socketFactoryRegistry);
HttpClients.custom().setConnectionManager(connManager);
//创建自定义的httpclient对象
CloseableHttpClientclient=HttpClients.custom().setConnectionManager(connManager).build();
//创建post方式请求对象
HttpPosthttpPost=newHttpPost(url);
//装填参数
Listnvps=newArrayList();
if(map!=null){
for(Map.Entryentry:map.entrySet()){
nvps.add(newBasicNameValuePair(entry.getKey(),entry.getValue()));
}
}
//设置参数到请求对象中
httpPost.setEntity(newUrlEncodedFormEntity(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;MSIE5.0;WindowsNT;DigExt)");
//执行请求操作,并拿到结果(同步阻塞)
CloseableHttpResponseresponse=null;
try{
response=client.execute(httpPost);
//获取结果实体
HttpEntityentity=response.getEntity();
if(entity!=null){
//按指定编码转换结果实体为String类型
body=EntityUtils.toString(entity,encoding);
}
EntityUtils.consume(entity);
}catch(IOExceptione){
e.printStackTrace();
}finally{
try{
//释放链接
response.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
returnbody;
}
分 -->
|