<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    stone2083

    httpclient ssl support

    手頭上一些工作,需要經(jīng)常訪問公司內(nèi)網(wǎng)的數(shù)據(jù),為了減少重復(fù)的勞動(dòng)力,就考慮程序幫忙爬取信息數(shù)據(jù)。
    apache下httpclient是一個(gè)很好的工具,不過公司內(nèi)網(wǎng)是使用HTTPS協(xié)議的,于是乎,就需要考慮如何讓httpclient支持https。
    支持ssl的關(guān)鍵,在于如何創(chuàng)建一個(gè)SSLSocket,幸好,httpclient提供了支持。

    請(qǐng)看:
    org.apache.commons.httpclient.protocol.ProtocolSocketFactory
    javadocs:A factory for creating Sockets.

    實(shí)現(xiàn)一個(gè)簡(jiǎn)單的EasySSLProtocolSocketFactory,詳見代碼:
    public class EasySSLProtocolSocketFactory implements ProtocolSocketFactory {

        
    private SSLContext sslcontext = null;

        
    private String     ksfile;
        
    private String     tksfile;
        
    private String     kspwd;
        
    private String     tkspwd;

        
    public EasySSLProtocolSocketFactory(String ks, String kspwd, String tks, String tkspwd){
            
    this.ksfile = ks;
            
    this.kspwd = kspwd;
            
    this.tksfile = tks;
            
    this.tkspwd = tkspwd;
        }

        
    public Socket createSocket(String host, int port, InetAddress clientHost, int clientPort) throws IOException,
                                                                                                 UnknownHostException {
            
    return getSSLContext().getSocketFactory().createSocket(host, port, clientHost, clientPort);
        }

        
    public Socket createSocket(final String host, final int port, final InetAddress localAddress, final int localPort,
                                   
    final HttpConnectionParams params) throws IOException, UnknownHostException,
                                                                     ConnectTimeoutException {
            
    if (params == null) {
                
    throw new IllegalArgumentException("Parameters may not be null");
            }
            
    int timeout = params.getConnectionTimeout();
            SocketFactory socketfactory 
    = getSSLContext().getSocketFactory();
            
    if (timeout == 0) {
                
    return socketfactory.createSocket(host, port, localAddress, localPort);
            } 
    else {
                Socket socket 
    = socketfactory.createSocket();
                SocketAddress localaddr 
    = new InetSocketAddress(localAddress, localPort);
                SocketAddress remoteaddr 
    = new InetSocketAddress(host, port);
                socket.bind(localaddr);
                socket.connect(remoteaddr, timeout);
                
    return socket;
            }
        }

        
    public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
            
    return getSSLContext().getSocketFactory().createSocket(host, port);
        }

        
    public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException,
                                                                                           UnknownHostException {
            
    return getSSLContext().getSocketFactory().createSocket(socket, host, port, autoClose);
        }

        
    private SSLContext createEasySSLContext() {
            
    try {
                SSLContext context 
    = SSLContext.getInstance("SSL");

                KeyManagerFactory kmf 
    = KeyManagerFactory.getInstance("SunX509");
                TrustManagerFactory tmf 
    = TrustManagerFactory.getInstance("SunX509");

                KeyStore ks 
    = KeyStore.getInstance("JKS");
                KeyStore tks 
    = KeyStore.getInstance("JKS");

                ks.load(
    new FileInputStream(ksfile), kspwd.toCharArray());
                tks.load(
    new FileInputStream(tksfile), tkspwd.toCharArray());

                kmf.init(ks, kspwd.toCharArray());
                tmf.init(tks);

                context.init(kmf.getKeyManagers(), tmf.getTrustManagers(), 
    null);
                
    return context;
            } 
    catch (Exception e) {
                
    throw new HttpClientError(e.toString());
            }
        }

        
    private SSLContext getSSLContext() {
            
    if (this.sslcontext == null) {
                
    this.sslcontext = createEasySSLContext();
            }
            
    return this.sslcontext;
        }

    }

    備注:
    至于ssl相關(guān)的知識(shí),和如何創(chuàng)建java key store,可見:
    SSL雙向認(rèn)證java實(shí)現(xiàn)


    如何使用,也簡(jiǎn)單:
    Protocol.registerProtocol("https", new Protocol("https", new EasySSLProtocolSocketFactory(KS_FILE, KS_PWD, TKS_FILE, TKS_PWD), 443))

    官方資料:
    http://hc.apache.org/httpclient-3.x/sslguide.html

    演示代碼:
    httpclient-ssl.zip

    posted on 2010-01-30 14:39 stone2083 閱讀(2911) 評(píng)論(0)  編輯  收藏 所屬分類: java

    主站蜘蛛池模板: 亚洲人成网站影音先锋播放| 亚洲色偷偷综合亚洲AV伊人蜜桃| 久久这里只精品99re免费| 亚洲视频一区网站| 热99re久久精品精品免费| 大学生一级特黄的免费大片视频 | 亚洲Aⅴ在线无码播放毛片一线天 亚洲avav天堂av在线网毛片 | yy6080久久亚洲精品| 亚洲人AV在线无码影院观看| 久久国产精品免费专区| 亚洲一卡2卡4卡5卡6卡在线99| 国产精品va无码免费麻豆| 亚洲精品永久在线观看| 亚洲中文字幕在线第六区| caoporm超免费公开视频| 精品亚洲综合在线第一区| 免费毛片在线看片免费丝瓜视频 | 亚洲国产精品嫩草影院| 亚洲国产精品无码成人片久久| 女人18毛片水最多免费观看 | 最新欧洲大片免费在线| 亚欧乱色国产精品免费视频| 中文字幕亚洲综合小综合在线| 德国女人一级毛片免费| 国产免费无码AV片在线观看不卡 | 亚洲已满18点击进入在线观看| 国产成人精品久久亚洲高清不卡 | 男女超爽刺激视频免费播放| 中文字幕高清免费不卡视频| 亚洲日韩国产二区无码| 亚洲精品456在线播放| 日韩高清在线免费观看| 亚洲精品国产日韩无码AV永久免费网 | 久久国产乱子伦精品免费强| 美女视频黄频a免费大全视频| 亚洲一区精品伊人久久伊人| 毛片a级毛片免费观看免下载| 狠狠热精品免费观看| 亚洲影视自拍揄拍愉拍| 亚洲最新永久在线观看| 亚洲综合无码AV一区二区|