SSLContext 去除证书验证
来源:互联网 发布:知商金融 红包 编辑:程序博客网 时间:2024/06/05 23:48
private static Logger log = LoggerFactory.getLogger(CommonUtil.class); private static final String APPLICATION_JSON = "application/json"; private static final String CONTENT_TYPE_TEXT_JSON = "text/json"; private static final String CONTENT_TYPE_TEXT_XML = "text/xml"; private static final String APPLICATION_XML = "application/xml";public static String doGetString(String url, String charSet) throws Exception {String result = ""; HttpGet httpGet = new HttpGet(url);//此处跳过证书验证的方式适用于apache httpclient 4.3.x版本,并不一定适用于其他httpclient版本,请注意。SSLContext sslContext = SSLContexts.custom().useTLS().loadTrustMaterial(null, new TrustStrategy() {public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {//信任所有return true;}}).build();SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext, new AllowAllHostnameVerifier()); Registry<ConnectionSocketFactory> r = RegistryBuilder.<ConnectionSocketFactory>create().register("https", sslConnectionSocketFactory).build();//设置连接池,配置过期时间为20s。PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(r);cm.setMaxTotal(500);cm.setDefaultMaxPerRoute(350);SocketConfig socketConfig = SocketConfig.custom().setSoKeepAlive(true).setTcpNoDelay(true).setSoTimeout(20000).build();cm.setDefaultSocketConfig(socketConfig);RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(20000).setConnectTimeout(20000).setSocketTimeout(20000).build();//创建httpClientCloseableHttpClient httpclient= HttpClients.custom().setConnectionManager(cm).setDefaultRequestConfig(requestConfig).build(); try {// 使用DefaultHttpClient类的execute方法发送HTTP GET请求,并返回HttpResponse对象。HttpResponse httpResponse = httpclient.execute(httpGet);// 其中HttpGet是HttpUriRequst的子类if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {httpGet.abort();httpclient.close();return result;}HttpEntity httpEntity = httpResponse.getEntity();result = EntityUtils.toString(httpEntity,charSet);// 取出应答字符串// 一般来说都要删除多余的字符result.replaceAll("\r", "");// 去掉返回结果中的"\r"字符,否则会在结果字符串后面显示一个小方格} catch (ClientProtocolException e) {e.printStackTrace();result = e.getMessage().toString();} catch (IOException e) {e.printStackTrace();result = e.getMessage().toString();}finally{// 关闭连接,释放资源 try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); result = e.getMessage().toString();} }return result;} public static JSONObject doPOST(String requestUrl, String charSet) throws Exception { JSONObject jsonObject = null; HttpPost httpPost = new HttpPost(requestUrl); //此处跳过证书验证的方式适用于apache httpclient 4.3.x版本,并不一定适用于其他httpclient版本,请注意。 SSLContext sslContext = SSLContexts.custom().useTLS().loadTrustMaterial(null, new TrustStrategy() { public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { //信任所有 return true; } }).build(); SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext, new AllowAllHostnameVerifier()); Registry<ConnectionSocketFactory> r = RegistryBuilder.<ConnectionSocketFactory>create() .register("https", sslConnectionSocketFactory) .build(); //设置连接池,配置过期时间为20s。 PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(r); cm.setMaxTotal(500); cm.setDefaultMaxPerRoute(350); SocketConfig socketConfig = SocketConfig.custom() .setSoKeepAlive(true) .setTcpNoDelay(true) .setSoTimeout(20000) .build(); cm.setDefaultSocketConfig(socketConfig); RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(20000).setConnectTimeout(20000).setSocketTimeout(20000).build(); //创建httpClient CloseableHttpClient httpclient= HttpClients.custom() .setConnectionManager(cm) .setDefaultRequestConfig(requestConfig) .build(); try { // 使用DefaultHttpClient类的execute方法发送HTTP GET请求,并返回HttpResponse对象。 HttpResponse httpResponse = httpclient.execute(httpPost);// 其中HttpGet是HttpUriRequst的子类 if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { httpPost.abort(); httpclient.close(); jsonObject = JSONObject.fromObject(result); return jsonObject; } HttpEntity httpEntity = httpResponse.getEntity(); result = EntityUtils.toString(httpEntity,charSet);// 取出应答字符串 // 一般来说都要删除多余的字符 result.replaceAll("\r", "");// 去掉返回结果中的"\r"字符,否则会在结果字符串后面显示一个小方格 } catch (ClientProtocolException e) { e.printStackTrace(); result = e.getMessage().toString(); } catch (IOException e) { e.printStackTrace(); result = e.getMessage().toString(); }finally{ // 关闭连接,释放资源 try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); result = e.getMessage().toString(); } } jsonObject = JSONObject.fromObject(result); return jsonObject; }
阅读全文
0 0
- SSLContext 去除证书验证
- SSLContext
- java SSLContext
- java SSLContext
- java SSLContext
- 去除office 正版验证
- 去除windows验证通知
- eclipse 去除XML验证
- Eclipse去除js验证
- Salesforce去除邮箱验证
- 去除Eslint验证
- 登录证书验证
- PuTTY使用证书验证
- 验证证书的过程
- java安全证书验证
- openssl verify 验证证书
- 登录证书验证 .
- SSL证书验证详解
- Spring Boot 依赖POM
- Spring 利用logback输出打印日志:
- 管道通信
- 内部人士揭秘:美国退出教科文组织,可不只不想交钱这么简单
- Linux vdso的由来
- SSLContext 去除证书验证
- int a[60][250][1000]数组的遍历
- Mac环境下VMware Fusion虚拟机虚拟网卡的配置
- linux文件系统详解
- 2>&1 的用法说明。
- Struts2简单开发流程
- LLVM学习笔记(20)
- HTTP协议详解(真的很经典)
- hadoop之 hadoop 机架感知