<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ù),為了減少重復的勞動力,就考慮程序幫忙爬取信息數(shù)據(jù)。
    apache下httpclient是一個很好的工具,不過公司內(nèi)網(wǎng)是使用HTTPS協(xié)議的,于是乎,就需要考慮如何讓httpclient支持https。
    支持ssl的關(guān)鍵,在于如何創(chuàng)建一個SSLSocket,幸好,httpclient提供了支持。

    請看:
    org.apache.commons.httpclient.protocol.ProtocolSocketFactory
    javadocs:A factory for creating Sockets.

    實現(xià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)的知識,和如何創(chuàng)建java key store,可見:
    SSL雙向認證java實現(xià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) 評論(0)  編輯  收藏 所屬分類: java

    主站蜘蛛池模板: 精品亚洲国产成AV人片传媒| 免费看少妇作爱视频| 中文字幕亚洲专区| 国产一级大片免费看| 亚洲欧美日韩中文字幕一区二区三区| 国产免费一区二区三区| 亚洲码一区二区三区| 久久水蜜桃亚洲AV无码精品| 在线免费一区二区| 亚洲av永久无码天堂网| 中文字幕乱码系列免费| 最近的中文字幕大全免费版| 国产亚洲精久久久久久无码| 亚洲精品国产综合久久久久紧 | 亚洲欧美中文日韩视频| 国产人在线成免费视频| 亚洲精品无码av人在线观看| 国产色爽免费无码视频| 亚洲人成亚洲精品| 大学生一级毛片免费看| 人人狠狠综合久久亚洲| 亚洲欧洲久久av| 在线观看亚洲AV日韩A∨| 性感美女视频免费网站午夜| 看Aⅴ免费毛片手机播放| 亚洲无码精品浪潮| 久久99精品视免费看| 亚洲免费黄色网址| 小小影视日本动漫观看免费| 亚洲日本香蕉视频观看视频| 两个人日本WWW免费版| 亚洲av丰满熟妇在线播放| 成年免费大片黄在线观看岛国| 美女无遮挡免费视频网站| 99久久综合国产精品免费| 国产AV无码专区亚洲AV蜜芽| 亚洲无av在线中文字幕| 国产精品久久久久免费a∨| 色婷婷综合缴情综免费观看| 亚洲人成依人成综合网| 日本人的色道www免费一区|