ice world

          There is nothing too difficult if you put your heart into it.
          posts - 104, comments - 103, trackbacks - 0, articles - 0
          本演示例程是繼Java Tomcat SSL 服務(wù)端/客戶端雙向認證(一),密鑰庫可證書的生成腳本不再重復(fù)黏貼,僅僅是用程序來代替瀏覽器訪問服務(wù)端。
          例程中使用到了Apache HttpClient庫,版本為4.1.3
          全部依賴庫:
          commons-logging-1.1.1.jar
          httpclient-4.1.3.jar
          httpcore-4.1.4.jar
          httpmime-4.1.3.jar(上傳文件使用)

          在(一)中的程序包中創(chuàng)建一個客戶端類:HttpsClient
          HttpsClient.java
          package com.icesoft.client;

          import java.io.BufferedReader;
          import java.io.File;
          import java.io.FileInputStream;
          import java.io.InputStream;
          import java.io.InputStreamReader;
          import java.security.KeyStore;

          import org.apache.http.HttpEntity;
          import org.apache.http.HttpResponse;
          import org.apache.http.client.HttpClient;
          import org.apache.http.client.methods.HttpGet;
          import org.apache.http.conn.scheme.Scheme;
          import org.apache.http.conn.ssl.SSLSocketFactory;
          import org.apache.http.impl.client.DefaultHttpClient;
          import org.apache.http.util.EntityUtils;

          public class HttpsClient {
             
             
          private static final String KEY_STORE_TYPE_JKS = "jks";
             
          private static final String KEY_STORE_TYPE_P12 = "PKCS12";
             
          private static final String SCHEME_HTTPS = "https";
             
          private static final int HTTPS_PORT = 8443;
             
          private static final String HTTPS_URL = "https://127.0.0.1:8443/HttpClientSSL/sslServlet";
             
          private static final String KEY_STORE_CLIENT_PATH = "E:/ssl/client.p12";
             
          private static final String KEY_STORE_TRUST_PATH = "E:/ssl/client.truststore";
             
          private static final String KEY_STORE_PASSWORD = "123456";
             
          private static final String KEY_STORE_TRUST_PASSWORD = "123456";

             
          public static void main(String[] args) throws Exception {
                  ssl();
              }

             
             
          private static void ssl() throws Exception {
                  HttpClient httpClient
          = new DefaultHttpClient();
                 
          try {
                      KeyStore keyStore 
          = KeyStore.getInstance(KEY_STORE_TYPE_P12);
                      KeyStore trustStore 
          = KeyStore.getInstance(KEY_STORE_TYPE_JKS);
                      InputStream ksIn
          = new FileInputStream(KEY_STORE_CLIENT_PATH);
                      InputStream tsIn
          = new FileInputStream(new File(KEY_STORE_TRUST_PATH));
                     
          try {
                          keyStore.load(ksIn, KEY_STORE_PASSWORD.toCharArray());
                          trustStore.load(tsIn, KEY_STORE_TRUST_PASSWORD.toCharArray());
                      }
          finally {
                         
          try { ksIn.close(); } catch (Exception ignore) {}
                         
          try { tsIn.close(); } catch (Exception ignore) {}
                      }

                      SSLSocketFactory socketFactory
          = new SSLSocketFactory(keyStore, KEY_STORE_PASSWORD, trustStore);
                      Scheme sch
          = new Scheme(SCHEME_HTTPS, HTTPS_PORT, socketFactory);
                      httpClient.getConnectionManager().getSchemeRegistry().register(sch);
                      HttpGet httpget
          = new HttpGet(HTTPS_URL);
                      System.out.println(
          "executing request" + httpget.getRequestLine());
                      HttpResponse response
          = httpClient.execute(httpget);
                      HttpEntity entity
          = response.getEntity();
                      System.out.println(
          "----------------------------------------");
                      System.out.println(response.getStatusLine());
                     
          if (entity != null) {
                          System.out.println(
          "Response content length: " + entity.getContentLength());
                          BufferedReader bufferedReader
          = new BufferedReader(new InputStreamReader(entity.getContent()));
                          String text;
                         
          while ((text = bufferedReader.readLine()) != null) {
                              System.out.println(text);
                          }

                          bufferedReader.close();
                      }

                      EntityUtils.consume(entity);
                  }
          finally {
                      httpClient.getConnectionManager().shutdown();
                  }

              }


          }



          啟動Tomcat,運行HttpsClient,控制臺返回:


          OK,和使用瀏覽器訪問得到的結(jié)果一模一樣!

          全文完!

          Feedback

          # re: Java Tomcat SSL 服務(wù)端/客戶端雙向認證のApache HttpClient(二)  回復(fù)  更多評論   

          2013-05-14 10:01 by zhyg
          為什么我的一直是“這是一個HTTPS請求,但是沒有可用的客戶端證書”?
          主站蜘蛛池模板: 明溪县| 德惠市| 子洲县| 万年县| 白银市| 班玛县| 城步| 隆昌县| 三都| 南宁市| 南阳市| 尼勒克县| 眉山市| 汉川市| 明光市| 哈密市| 新巴尔虎左旗| 内黄县| 泾阳县| 嘉兴市| 兴仁县| 陵川县| 龙南县| 错那县| 延长县| 临潭县| 延边| 大同县| 朝阳市| 庐江县| 林州市| 武夷山市| 溆浦县| 舒城县| 盱眙县| 阿巴嘎旗| 股票| 双流县| 开远市| 惠州市| 霍邱县|