package com.pccw.hktdcb.apiserver.util; import it.sauronsoftware.base64.Base64; import java.io.IOException; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLException; import javax.net.ssl.SSLSession; import javax.net.ssl.SSLSocket; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.conn.ClientConnectionManager; import org.apache.http.conn.scheme.PlainSocketFactory; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.scheme.SchemeRegistry; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.conn.ssl.X509HostnameVerifier; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.CoreConnectionPNames; import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class HttpsRequestUtil { protected static final Logger logger = LoggerFactory .getLogger(HttpsRequestUtil.class); private static final int TIME_OUT = Integer.parseInt(SystemConfig .getProperty("https_client_timeout")) * 1000; public static HttpClient getHttpClient() { @SuppressWarnings("deprecation") HttpClient httpclient = new DefaultHttpClient(); try { // Secure Protocol implementation. SSLContext ctx = SSLContext.getInstance("SSL"); // Implementation of a trust manager for X509 certificates X509TrustManager tm = new X509TrustManager() { public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException { } public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException { } public X509Certificate[] getAcceptedIssuers() { return null; } }; X509HostnameVerifier hostnameVerifier = new X509HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { // TODO Auto-generated method stub return true; } public void verify(String arg0, SSLSocket arg1) throws IOException { // TODO Auto-generated method stub } public void verify(String arg0, X509Certificate arg1) throws SSLException { // TODO Auto-generated method stub } public void verify(String arg0, String[] arg1, String[] arg2) throws SSLException { // TODO Auto-generated method stub } }; ctx.init(null, new TrustManager[] { tm }, null); SSLSocketFactory ssf = new SSLSocketFactory(ctx, hostnameVerifier); ClientConnectionManager ccm = httpclient.getConnectionManager(); // register https protocol in httpclient's scheme registry SchemeRegistry sr = ccm.getSchemeRegistry(); sr.register(new Scheme("https", 443, ssf)); sr.register(new Scheme("http", 80, PlainSocketFactory .getSocketFactory())); // set Time out httpclient.getParams().setParameter( CoreConnectionPNames.CONNECTION_TIMEOUT, TIME_OUT); httpclient.getParams().setParameter( CoreConnectionPNames.SO_TIMEOUT, TIME_OUT); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (KeyManagementException e) { // TODO Auto-generated catch block e.printStackTrace(); } return httpclient; } public static String getHTTPSRequest(String url, String userName, String password) { logger.info("HttpsRequestUtil : getHTTPSRequest start............"); logger.debug("url : " + url); String result = null; HttpGet httpget = new HttpGet(url); try { String authString = userName + ":" + password; String authStringEnc = new String(Base64.encode(authString .getBytes())); httpget.addHeader("Authorization", "Basic " + authStringEnc); httpget.addHeader("content-type", "application/json"); httpget.addHeader("accept", "application/json"); HttpClient httpclient = getHttpClient(); HttpResponse response = httpclient.execute(httpget); if (response != null) { HttpEntity resEntity = response.getEntity(); if (resEntity != null) { int httpStatus = response.getStatusLine().getStatusCode(); if (httpStatus == HttpStatus.SC_OK) { result = EntityUtils.toString(resEntity, "UTF-8"); } else { logger.error( "HttpsRequestUtil : getHTTPSRequest failed, http status code is [{}]", httpStatus); } } } logger.debug("HttpsRequestUtil : getHTTPSRequest;result = " + result); } catch (ClientProtocolException e) { logger.error( "HttpsRequestUtil : getHTTPSRequest error occur, Exception is [{}]", e.getMessage()); } catch (IOException e) { logger.error( "HttpsRequestUtil : getHTTPSRequest error occur, Exception is [{}]", e.getMessage()); } finally { httpget.releaseConnection(); } logger.info("HttpsRequestUtil : getHTTPSRequest end........"); return result; } }
相关推荐
http远程接口调用-httpClient+跳过SSL证书校验. 里面分别有3个httpClient的工具类代码。 还有一个专门为了跳过SSL证书校验的工具类。
Https跳过安全认证SSL,用于android开发,不常用协议连接
Java SpringBoot 项目跳过 ssl 证书认证文件
CXF实现SSL安全验证,实现https的WebService
主要介绍了Java跳过https的ssl证书验证的解决思路,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,下面我们来深入学习下吧
基于java的HTTPS模拟上传,只有一个类和一个测试类。主要解决java模拟https上传文件使用
java的get和post请求,获取json的工具类,https时会存在ssl校验的问题,工具会自动去除ssl校验。
Https跳过安全认证SSL,用于android开发,不常用协议连接.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
Java Https请求,跳过证书,直接访问 相关jar包全有 直接下载就能调试
使用HttpClient4.5实现https请求忽略SSL证书验证工具类
两路认证(也称为两路tls,两路ssl,相互认证):客户端和对方验证证书的Https连接,也称为相互认证 有用的链接 一些历史 我主要使用Apache Http Client,因此最初仅使用来自Apache的http客户端创建了该项目。 一段...
它还支持设置请求头、发送表单数据、上传文件、自动跟随重定向、跳过SSL证书验证等功能。Curl的语法简单易懂,可以快速学习和上手。由于Curl是开源的,因此可以在各种操作系统上使用。它是一个非常有用的工具,可以...
轴工具 使用Apache Axis 1.4进行动态SSL配置的实用程序类 从下载并转换为Maven项目。
2、新增跳过证书的类,TrustAllTrustManager.java,代码如下: public class TrustAllTrustManager implements javax.net.ssl.TrustManager, javax.net.ssl.X509TrustManager { @Override public java.security....
1、花钱向认证机构购买的证书,(我们公司买的证书一个就需要4000元,TMD,还不如多租一台服务器)。服务器如果使用了此类证书的话,那对于移动端来说,直接可以忽略此证书,直接用https访问。与之不同的是ios内置了...
跳过列出优先级和使用先进的过滤规则 集成 SFV 校验和验证 直接查看/编辑远程文件 高级默认的“文件存在”行动的配置 过滤器显示为您的文件/目录的类型 支持安全的站点到站点使用SSCN转移 要求 下列软件包需要在您...
kong-oauth2-demo 这是一个与Kong oauth2插件一起使用的简单演示,显示了4种不同的授权流程我还在fomm/kong-oauth2-demo制作了一个fomm/kong-oauth2-demo镜像您可以使用docker run ... 默认情况下,此工具跳过ssl验证。
:high_voltage: 一个用Rust... 跳过相对网址 包含图案 排除模式 处理重定向 忽略不安全的SSL 文件遍历 限价方案 自定义标题 概括 HEAD要求 彩色输出 过滤器状态码 自定义超时 电邮连结 进度条 重试和
覆盖Rules-Configure一种上传软件,以确定现有文件匹配一个被转移需要重写,重命名,或跳过基于日期、大小、和其他标准,或选择促使每次找到一个匹配的文件。 Auto-Rename Transfers-Create规则更改一个文件的名字,扩展,...
4.8 跳过可迭代对象的开始部分 4.9 排列组合的迭代 4.10 序列上索引值迭代 4.11 同时迭代多个序列 4.12 不同集合上元素的迭代 4.13 创建数据处理管道 4.14 展开嵌套的序列 4.15 顺序迭代合并后的排序迭代...