posts - 66,  comments - 40,  trackbacks - 0

          package test;

          import java.io.IOException;
          import java.net.InetAddress;
          import java.net.Socket;
          import java.net.UnknownHostException;

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

          import org.apache.commons.httpclient.ConnectTimeoutException;
          import org.apache.commons.httpclient.HttpClientError;
          import org.apache.commons.httpclient.params.HttpConnectionParams;
          import org.apache.commons.httpclient.protocol.ControllerThreadSocketFactory;
          import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;

          public class MySecureProtocolSocketFactory implements
          ??SecureProtocolSocketFactory {

          ?private SSLContext sslContext = null;

          ?/**
          ? * Constructor for MySecureProtocolSocketFactory.
          ? */
          ?public MySecureProtocolSocketFactory() {
          ?}

          ?/**
          ? *
          ? * @return
          ? */
          ?private static SSLContext createEasySSLContext() {
          ??try {
          ???SSLContext context = SSLContext.getInstance("SSL");
          ???context.init(null, new TrustManager[] { new MyX509TrustManager() },
          ?????null);
          ???return context;
          ??} catch (Exception e) {
          ???throw new HttpClientError(e.toString());
          ??}
          ?}

          ?/**
          ? *
          ? * @return
          ? */
          ?private SSLContext getSSLContext() {
          ??if (this.sslContext == null) {
          ???this.sslContext = createEasySSLContext();
          ??}
          ??return this.sslContext;
          ?}

          ?/*
          ? * (non-Javadoc)
          ? *
          ? * @see org.apache.commons.httpclient.protocol.ProtocolSocketFactory#createSocket(java.lang.String,
          ? *????? int, java.net.InetAddress, int)
          ? */
          ?public Socket createSocket(String host, int port, InetAddress clientHost,
          ???int clientPort) throws IOException, UnknownHostException {

          ??return getSSLContext().getSocketFactory().createSocket(host, port,
          ????clientHost, clientPort);
          ?}

          ?/*
          ? * (non-Javadoc)
          ? *
          ? * @see org.apache.commons.httpclient.protocol.ProtocolSocketFactory#createSocket(java.lang.String,
          ? *????? int, java.net.InetAddress, int,
          ? *????? org.apache.commons.httpclient.params.HttpConnectionParams)
          ? */
          ?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();
          ??if (timeout == 0) {
          ???return createSocket(host, port, localAddress, localPort);
          ??} else {???
          ???return ControllerThreadSocketFactory.createSocket(this, host, port,
          ?????localAddress, localPort, timeout);
          ??}
          ?}

          ?/*
          ? * (non-Javadoc)
          ? *
          ? * @see SecureProtocolSocketFactory#createSocket(java.lang.String,int)
          ? */
          ?public Socket createSocket(String host, int port) throws IOException,
          ???UnknownHostException {
          ??return getSSLContext().getSocketFactory().createSocket(host, port);
          ?}

          ?/*
          ? * (non-Javadoc)
          ? *
          ? * @see SecureProtocolSocketFactory#createSocket(java.net.Socket,java.lang.String,int,boolean)
          ? */
          ?public Socket createSocket(Socket socket, String host, int port,
          ???boolean autoClose) throws IOException, UnknownHostException {
          ??return getSSLContext().getSocketFactory().createSocket(socket, host,
          ????port, autoClose);
          ?}
          }

          posted on 2007-01-18 09:52 happytian 閱讀(687) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2007年1月>
          31123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          Welcome here, my friend!

          常用鏈接

          留言簿(12)

          隨筆檔案(66)

          文章分類

          文章檔案(63)

          web

          最新隨筆

          搜索

          •  

          積分與排名

          • 積分 - 89327
          • 排名 - 647

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 建德市| 盘山县| 寿阳县| 墨竹工卡县| 城市| 定远县| 云龙县| 阳东县| 永善县| 墨玉县| 岗巴县| 金溪县| 讷河市| 陕西省| 北流市| 阜新市| 眉山市| 琼中| 罗源县| 邻水| 汾阳市| 临朐县| 德格县| 新平| 阳高县| 青州市| 东阿县| 四川省| 泸西县| 灵台县| 南投市| 景泰县| 北辰区| 叶城县| 佛坪县| 聊城市| 平潭县| 衡水市| 天等县| 霍山县| 武威市|