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ù)端/客戶(hù)端雙向認(rèn)證(一),密鑰庫(kù)可證書(shū)的生成腳本不再重復(fù)黏貼,僅僅是用程序來(lái)代替瀏覽器訪(fǎng)問(wèn)服務(wù)端。
          例程中使用到了Apache HttpClient庫(kù),版本為4.1.3
          全部依賴(lài)庫(kù):
          commons-logging-1.1.1.jar
          httpclient-4.1.3.jar
          httpcore-4.1.4.jar
          httpmime-4.1.3.jar(上傳文件使用)

          在(一)中的程序包中創(chuàng)建一個(gè)客戶(hù)端類(lèi):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();
                  }

              }


          }



          啟動(dòng)Tomcat,運(yùn)行HttpsClient,控制臺(tái)返回:


          OK,和使用瀏覽器訪(fǎng)問(wèn)得到的結(jié)果一模一樣!

          全文完!

          Feedback

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

          2013-05-14 10:01 by zhyg
          為什么我的一直是“這是一個(gè)HTTPS請(qǐng)求,但是沒(méi)有可用的客戶(hù)端證書(shū)”?
          主站蜘蛛池模板: 锡林郭勒盟| 洪江市| 无为县| 青川县| 贵定县| 上高县| 汪清县| 丽江市| 彩票| 绥中县| 旌德县| 辰溪县| 汕尾市| 涿鹿县| 湖南省| 安阳市| 夏津县| 长汀县| 永春县| 于都县| 四子王旗| 阿拉善左旗| 景谷| 景东| 靖西县| 新民市| 固安县| 谢通门县| 册亨县| 民权县| 武安市| 苍梧县| 潜山县| 乐山市| 西乌珠穆沁旗| 阿坝县| 阿尔山市| 平江县| 芜湖市| 仁化县| 灵台县|