Java,Jsp,模式及框架
Web技术
Web服务器
浏览器相关
SQL语言
数据库
开发环境
软件开发及管理
网站SEO
短信及邮件服务
网页设计
电脑、硬件及网络
协同管理平台问题
电子商务
前沿技术及趋势
  当前位置:首页 - 知识积累 - Java,Jsp,模式及框架
httpclient抓取https协议网页的方法
时间:2013年12月20日 

首先需要新增以下类:

import java.security.cert.CertificateException;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;


import org.apache.http.client.HttpClient;
import org.apache.http.conn.ClientConnectionManager;
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.impl.client.DefaultHttpClient;

public class WebClientDevWrapper {


 public static DefaultHttpClient wrapClient(HttpClient base) {
  try {
   SSLContext ctx = SSLContext.getInstance("TLS");
   X509TrustManager tm = new X509TrustManager() {

    //public X509Certificate[] getAcceptedIssuers() {
    public java.security.cert.X509Certificate[] getAcceptedIssuers() {
     return null;
    }

    @Override
    public void checkClientTrusted(
      java.security.cert.X509Certificate[] chain,
      String authType) throws CertificateException {
     // TODO Auto-generated method stub
     
    }

    @Override
    public void checkServerTrusted(
      java.security.cert.X509Certificate[] chain,
      String authType) throws CertificateException {
     // TODO Auto-generated method stub     
    }
   };
   ctx.init(null, new TrustManager[]{tm}, null);
   SSLSocketFactory ssf = new SSLSocketFactory(ctx);
   ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
   ClientConnectionManager ccm = base.getConnectionManager();
   SchemeRegistry sr = ccm.getSchemeRegistry();
   sr.register(new Scheme("https", ssf, 443));
   return new DefaultHttpClient(ccm, base.getParams());
  } catch (Exception ex) {
   ex.printStackTrace();
   return null;
  }

 }
}

 

然后使用以下语句处理httpclient

HttpClient httpClient = new DefaultHttpClient();
httpClient=WebClientDevWrapper.wrapClient(httpClient);

 接下来就可以使用httpClient抓取网页了。