通用http、https访问工具类
来源:互联网 发布:中学生免费辅导软件 编辑:程序博客网 时间:2024/06/04 19:20
通用http、https访问工具类
对于https的访问,需要信任服务器端的证书。
public class HttpHelper{
public static HttpURLConnection getConnection(String urlStr) throws KeyManagementException, MalformedURLException, NoSuchAlgorithmException, IOException{
HttpURLConnection conn = null;
if (urlStr.toLowerCase().startsWith("https"))
conn = getHttpsConnection(urlStr);
else
conn = getHttpConnection(urlStr);
return conn;
}
private static HttpURLConnection getHttpConnection(String urlStr) throws MalformedURLException, IOException {
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
return conn;
}
private static HttpsURLConnection getHttpsConnection(String urlStr) throws MalformedURLException, IOException,NoSuchAlgorithmException, KeyManagementException {
URL url = new URL(urlStr);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setHostnameVerifier(new IgnoreHostnameVerifier());
TrustManager[] tm = { new IgnoreCertificationTrustManger() };
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, tm, null);
SSLSocketFactory ssf = sslContext.getSocketFactory();
conn.setSSLSocketFactory(ssf);
return conn;
}
}
public class IgnoreCertificationTrustManger implements X509TrustManager {
private X509Certificate[] certificates;
public void checkClientTrusted(X509Certificate certificates[],
String authType) throws CertificateException {
if (this.certificates == null) {
this.certificates = certificates;
}
}
public void checkServerTrusted(X509Certificate[] ax509certificate, String s)
throws CertificateException {
if (this.certificates == null) {
this.certificates = ax509certificate;
}
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
}
public class IgnoreHostnameVerifier implements HostnameVerifier{
public boolean verify(String arg0, SSLSession arg1) {
return true;
}
}
public class HttpHelper{
public static HttpURLConnection getConnection(String urlStr) throws KeyManagementException, MalformedURLException, NoSuchAlgorithmException, IOException{
HttpURLConnection conn = null;
if (urlStr.toLowerCase().startsWith("https"))
conn = getHttpsConnection(urlStr);
else
conn = getHttpConnection(urlStr);
return conn;
}
private static HttpURLConnection getHttpConnection(String urlStr) throws MalformedURLException, IOException {
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
return conn;
}
private static HttpsURLConnection getHttpsConnection(String urlStr) throws MalformedURLException, IOException,NoSuchAlgorithmException, KeyManagementException {
URL url = new URL(urlStr);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setHostnameVerifier(new IgnoreHostnameVerifier());
TrustManager[] tm = { new IgnoreCertificationTrustManger() };
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, tm, null);
SSLSocketFactory ssf = sslContext.getSocketFactory();
conn.setSSLSocketFactory(ssf);
return conn;
}
}
public class IgnoreCertificationTrustManger implements X509TrustManager {
private X509Certificate[] certificates;
public void checkClientTrusted(X509Certificate certificates[],
String authType) throws CertificateException {
if (this.certificates == null) {
this.certificates = certificates;
}
}
public void checkServerTrusted(X509Certificate[] ax509certificate, String s)
throws CertificateException {
if (this.certificates == null) {
this.certificates = ax509certificate;
}
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
}
public class IgnoreHostnameVerifier implements HostnameVerifier{
public boolean verify(String arg0, SSLSession arg1) {
return true;
}
}
- 通用http、https访问工具类
- http https请求工具类
- HTTP访问工具类
- http转https访问
- Apache配置http访问转https(widows和linux通用)
- http远程访问工具类
- IOS-访问HTTP和HTTPS
- Http 和 Https 访问请求
- http和https访问共存
- http、httpd、https、浏览工具
- nginx 配置https访问以及访问http跳转到https
- 基于HttpClient实现Http访问工具类
- post/get请求(http,https)工具类
- jsoup请求http或https返回json字符串工具类
- http https信任任何证书的工具类
- C# 调用http和https请求rest接口通用操作类
- .htaccess HTTP访问跳转至HTTPS
- delphi idhttp访问http与https不同点
- VC内存泄漏检测工具_Visual Leak Detector
- JAVASCRIPT拷贝内容到剪切板
- Oracle常见SQL分页测试方案
- Linux 中 RPM 命令参数使用详解
- iphone实现类似Android Grid效果的列表视图
- 通用http、https访问工具类
- MyEclipse8.5 开发环境配置中SVN插件安装方法
- gridview 英文 日期绑定 以及其他类型
- Linux下的通用打开命令
- SetInterpolationMode控制图形的缩放质量
- 二分检索 BinarySearch
- Struts2 SEOActionNameBuild (The action name cannot be the same as the action suffix [Action]) 终极解决方案
- 把链接加入收藏夹
- LR监控RedHat