添加Https协议兼容

 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);
		}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值