HTTP:是互联网上应用最为广泛的一种网络协议,是一个客户端和服务器端请求和应答的标准(TCP),用于从WWW服务器传输超文本到本地浏览器的传输协议,它可以使浏览器更加高效,使网络传输减少。
HTTPS:是以安全为目标的HTTP通道,简单讲是HTTP的安全版,即HTTP下加入SSL层,HTTPS的安全基础是SSL,因此加密的详细内容就需要SSL。
使用org.apache工具包連接,代码中添加Https协议兼容如下:
if(url.trim().startsWith("https:")) {
TrustManager easyTrustManager = new X509TrustManager() {
public void checkClientTrusted(java.security.cert.X509Certificate[] x509Certificates, String s)
throws java.security.cert.CertificateException {
}
public void checkServerTrusted(java.security.cert.X509Certificate[] x509Certificates, String s)
throws java.security.cert.CertificateException {
}
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[0]; //To change body of implemented methods use File | Settings | File Templates.
}
};
SSLContext sslcontext = SSLContext.getInstance(SSLSocketFactory.SSL);
sslcontext.init(null, new TrustManager[]{easyTrustManager}, null);
SSLSocketFactory sf = new SSLSocketFactory(sslcontext);
sf.setHostnameVerifier(new AllowAllHostnameVerifier());
Scheme sch = null;
if(port == 0) {
sch = new Scheme("https", 443, sf);
} else {
sch = new Scheme("https", port, sf);
}
httpClient.getConnectionManager().getSchemeRegistry().register(sch);
}