<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一免费| 香蕉视频免费在线| 99爱视频99爱在线观看免费| 亚洲日韩精品一区二区三区| 精品成人一区二区三区免费视频| 免费va在线观看| 免费一级特黄特色大片| 亚洲A丁香五香天堂网| 免费国产污网站在线观看不要卡| 免费h成人黄漫画嘿咻破解版| 四虎国产精品成人免费久久| 亚洲精品国产电影| 三级黄色免费观看| 亚洲影院在线观看| 在线观看H网址免费入口| 中国亚洲呦女专区| 免费a在线观看播放| 国产又黄又爽胸又大免费视频| 日韩亚洲欧洲在线com91tv| 88av免费观看| 亚洲成AV人影片在线观看| AV在线亚洲男人的天堂| 最近免费中文字幕MV在线视频3 | 亚洲国产成人一区二区精品区| 免费人成网站在线观看不卡| 亚洲男人天堂影院| 国产美女做a免费视频软件| 久久久久免费视频| 亚洲综合免费视频| 国产a级特黄的片子视频免费| 青青操视频在线免费观看| 亚洲精品视频在线观看免费| 最新猫咪www免费人成| 成人免费网站视频www| 亚洲2022国产成人精品无码区| 亚洲人成电影网站免费| 男人免费视频一区二区在线观看| 久久精品国产亚洲AV网站| 成人一a毛片免费视频| 岛国岛国免费V片在线观看| 亚洲制服在线观看|