The NoteBook of EricKong

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            611 Posts :: 1 Stories :: 190 Comments :: 0 Trackbacks

          常用鏈接

          留言簿(11)

          我參與的團隊

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

           有關tomcat 6.0如何配置https服務的文章可以參考:http://blog.csdn.net/zhou_zion/article/details/6759171
          以下主要講解如何使用https發起post請求:
          參考文檔:梁棟前輩的《Java加密與解密的藝術》
          [java] view plaincopy
          import java.io.BufferedReader;  
          import java.io.FileInputStream;  
          import java.io.IOException;  
          import java.io.InputStreamReader;  
          import java.net.MalformedURLException;  
          import java.net.URL;  
          import java.security.GeneralSecurityException;  
          import java.security.KeyStore;  
            
          import javax.net.ssl.HostnameVerifier;  
          import javax.net.ssl.HttpsURLConnection;  
          import javax.net.ssl.KeyManagerFactory;  
          import javax.net.ssl.SSLContext;  
          import javax.net.ssl.TrustManagerFactory;  
            
          public class HttpsPost {  
              /** 
               * 獲得KeyStore. 
               * @param keyStorePath 
               *            密鑰庫路徑 
               * @param password 
               *            密碼 
               * @return 密鑰庫 
               * @throws Exception 
               */  
              public static KeyStore getKeyStore(String password, String keyStorePath)  
                      throws Exception {  
                  // 實例化密鑰庫  
                  KeyStore ks = KeyStore.getInstance("JKS");  
                  // 獲得密鑰庫文件流  
                  FileInputStream is = new FileInputStream(keyStorePath);  
                  // 加載密鑰庫  
                  ks.load(is, password.toCharArray());  
                  // 關閉密鑰庫文件流  
                  is.close();  
                  return ks;  
              }  
            
              /** 
               * 獲得SSLSocketFactory. 
               * @param password 
               *            密碼 
               * @param keyStorePath 
               *            密鑰庫路徑 
               * @param trustStorePath 
               *            信任庫路徑 
               * @return SSLSocketFactory 
               * @throws Exception 
               */  
              public static SSLContext getSSLContext(String password,  
                      String keyStorePath, String trustStorePath) throws Exception {  
                  // 實例化密鑰庫  
                  KeyManagerFactory keyManagerFactory = KeyManagerFactory  
                          .getInstance(KeyManagerFactory.getDefaultAlgorithm());  
                  // 獲得密鑰庫  
                  KeyStore keyStore = getKeyStore(password, keyStorePath);  
                  // 初始化密鑰工廠  
                  keyManagerFactory.init(keyStore, password.toCharArray());  
            
                  // 實例化信任庫  
                  TrustManagerFactory trustManagerFactory = TrustManagerFactory  
                          .getInstance(TrustManagerFactory.getDefaultAlgorithm());  
                  // 獲得信任庫  
                  KeyStore trustStore = getKeyStore(password, trustStorePath);  
                  // 初始化信任庫  
                  trustManagerFactory.init(trustStore);  
                  // 實例化SSL上下文  
                  SSLContext ctx = SSLContext.getInstance("TLS");  
                  // 初始化SSL上下文  
                  ctx.init(keyManagerFactory.getKeyManagers(),  
                          trustManagerFactory.getTrustManagers(), null);  
                  // 獲得SSLSocketFactory  
                  return ctx;  
              }  
            
              /** 
               * 初始化HttpsURLConnection. 
               * @param password 
               *            密碼 
               * @param keyStorePath 
               *            密鑰庫路徑 
               * @param trustStorePath 
               *            信任庫路徑 
               * @throws Exception 
               */  
              public static void initHttpsURLConnection(String password,  
                      String keyStorePath, String trustStorePath) throws Exception {  
                  // 聲明SSL上下文  
                  SSLContext sslContext = null;  
                  // 實例化主機名驗證接口  
                  HostnameVerifier hnv = new MyHostnameVerifier();  
                  try {  
                      sslContext = getSSLContext(password, keyStorePath, trustStorePath);  
                  } catch (GeneralSecurityException e) {  
                      e.printStackTrace();  
                  }  
                  if (sslContext != null) {  
                      HttpsURLConnection.setDefaultSSLSocketFactory(sslContext  
                              .getSocketFactory());  
                  }  
                  HttpsURLConnection.setDefaultHostnameVerifier(hnv);  
              }  
            
              /** 
               * 發送請求. 
               * @param httpsUrl 
               *            請求的地址 
               * @param xmlStr 
               *            請求的數據 
               */  
              public static void post(String httpsUrl, String xmlStr) {  
                  HttpsURLConnection urlCon = null;  
                  try {  
                      urlCon = (HttpsURLConnection) (new URL(httpsUrl)).openConnection();  
                      urlCon.setDoInput(true);  
                      urlCon.setDoOutput(true);  
                      urlCon.setRequestMethod("POST");  
                      urlCon.setRequestProperty("Content-Length",  
                              String.valueOf(xmlStr.getBytes().length));  
                      urlCon.setUseCaches(false);  
                      //設置為gbk可以解決服務器接收時讀取的數據中文亂碼問題  
                      urlCon.getOutputStream().write(xmlStr.getBytes("gbk"));  
                      urlCon.getOutputStream().flush();  
                      urlCon.getOutputStream().close();  
                      BufferedReader in = new BufferedReader(new InputStreamReader(  
                              urlCon.getInputStream()));  
                      String line;  
                      while ((line = in.readLine()) != null) {  
                          System.out.println(line);  
                      }  
                  } catch (MalformedURLException e) {  
                      e.printStackTrace();  
                  } catch (IOException e) {  
                      e.printStackTrace();  
                  } catch (Exception e) {  
                      e.printStackTrace();  
                  }  
              }  
            
              /** 
               * 測試方法. 
               * @param args 
               * @throws Exception 
               */  
              public static void main(String[] args) throws Exception {  
                  // 密碼  
                  String password = "123456";  
                  // 密鑰庫  
                  String keyStorePath = "tomcat.keystore";  
                  // 信任庫  
                  String trustStorePath = "tomcat.keystore";  
                  // 本地起的https服務  
                  String httpsUrl = "https://localhost:8443/service/httpsPost";  
                  // 傳輸文本  
                  String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><fruitShop><fruits><fruit><kind>蘿卜</kind></fruit><fruit><kind>菠蘿</kind></fruit></fruits></fruitShop>";  
                  HttpsPost.initHttpsURLConnection(password, keyStorePath, trustStorePath);  
                  // 發起請求  
                  HttpsPost.post(httpsUrl, xmlStr);  
              }  
          }  
          posted on 2015-03-08 14:13 Eric_jiang 閱讀(356) 評論(0)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 江阴市| 读书| 安乡县| 丰都县| 乌拉特中旗| 武功县| 星子县| 锡林浩特市| 射阳县| 平利县| 上蔡县| 彭州市| 临夏市| 池州市| 民丰县| 柳林县| 格尔木市| 炉霍县| 大厂| 大邑县| 山东省| 枞阳县| 罗田县| 长治县| 墨玉县| 特克斯县| 绥中县| 康马县| 虞城县| 平陆县| 当阳市| 建德市| 延川县| 扬中市| 辰溪县| 莎车县| 元朗区| 三明市| 江西省| 揭东县| 尖扎县|