httpsclient 自動擷取認證 無認證訪問

來源:互聯網
上載者:User

標籤:

首先實現寫一個 實現介面SecureProtocolSocketFactory的類。

/** *ClassName: bcde *date: 2015年2月26日 下午4:51:01 * *@author limh *@since JDK 1.7 */import java.io.IOException;import java.net.InetAddress;import java.net.InetSocketAddress;import java.net.Socket;import java.net.SocketAddress;import java.net.UnknownHostException;import java.security.KeyManagementException;import java.security.NoSuchAlgorithmException;import java.security.cert.CertificateException;import java.security.cert.X509Certificate;import javax.net.SocketFactory;import javax.net.ssl.SSLContext;import javax.net.ssl.TrustManager;import javax.net.ssl.X509TrustManager;import org.apache.commons.httpclient.ConnectTimeoutException;import org.apache.commons.httpclient.params.HttpConnectionParams;import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;/** * MySecureProtocolSocketFactory.java.java Create on 2012-9-26下午1:15:03 *  *  * Copyright (c) 2012 by MTA. *  * @author lmeteor * @Email [email protected] * @description 自訂的socket factory 實現自動接受認證 * @version 1.0 */public class MySecureProtocolSocketFactory implements        SecureProtocolSocketFactory {    private SSLContext sslcontext = null;    private SSLContext createSSLContext() {        SSLContext sslcontext = null;        try {            sslcontext = SSLContext.getInstance("SSL");            sslcontext.init(null,                    new TrustManager[] { new TrustAnyTrustManager() },                    new java.security.SecureRandom());        } catch (NoSuchAlgorithmException e) {            e.printStackTrace();        } catch (KeyManagementException e) {            e.printStackTrace();        }        return sslcontext;    }    private SSLContext getSSLContext() {        if (this.sslcontext == null) {            this.sslcontext = createSSLContext();        }        return this.sslcontext;    }    public Socket createSocket(Socket socket, String host, int port,            boolean autoClose) throws IOException, UnknownHostException {        return getSSLContext().getSocketFactory().createSocket(socket, host,                port, autoClose);    }    public Socket createSocket(String host, int port) throws IOException,            UnknownHostException {        return getSSLContext().getSocketFactory().createSocket(host, port);    }    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(String host, int port, InetAddress localAddress,            int localPort, 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;        }    }    // 自訂私人類    private static class TrustAnyTrustManager implements X509TrustManager {        public void checkClientTrusted(X509Certificate[] chain, String authType)                throws CertificateException {        }        public void checkServerTrusted(X509Certificate[] chain, String authType)                throws CertificateException {        }        public X509Certificate[] getAcceptedIssuers() {            return new X509Certificate[] {};        }    }}

 

調用時,只需要在產生 httpclient之前,註冊剛才建立的https 協議對象,之後 httpsClient的用法正常使用即可。

ProtocolSocketFactory fcty = new MySecureProtocolSocketFactory();//註冊剛才建立的https 協議對象Protocol.registerProtocol("https", new Protocol("https", fcty, 443));HttpClient httpclient =  new HttpClient(); //httpsClient的用法正常使用PostMethod method = new PostMethod(URL);httpclient.executeMethod(method);

 

httpsclient 自動擷取認證 無認證訪問

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.