JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Application Server » Tomcat  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友   
reply to postflat modego to previous topicgo to next topic
己加入精華區
by koji at 2007-12-12 17:25
本主題所含的標籤
無標籤
作者 Re:Tomcat SSL 問題 [Re:secretguest0824]
secretguest0824

別當出頭鳥



發文: 354
積分: 1
於 2006-12-26 17:45 user profilesend a private message to usersend email to secretguest0824reply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
各位大大:
經過數十次嘗試後,總算在The Java Developers ALMANAC 1.4,Volume 1這本書找到答案,
將整個method程式碼公佈如下,供各位大大參考,
主要就是要先忽略對憑證的檢查,至於這樣Tomcat的keystore是否還要匯入跟憑證?還要在測試...:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
  /*
   * 以SSL傳送檔案
   */
  public boolean SendSSL(File file, String strSuccessMessage)
      throws NetworkErrorException {
    boolean boolRetrun = false;
    try {
      //
      // Disabling Certificate Validation in an HTTPConnection
      //
      // Create a trust manager that does not validate certificate chains
      TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
          return null;
        }
 
        public void checkClientTrusted(
            java.security.cert.X509Certificate[] certs,
            String authType) {
        }
 
        public void checkServerTrusted(
            java.security.cert.X509Certificate[] certs,
            String authType) {
        }
      } };
      // Install the all-trusting trust manager
      SSLContext sc = SSLContext.getInstance("SSL");
      sc.init(null, trustAllCerts, new java.security.SecureRandom());
      HttpsURLConnection
          .setDefaultSSLSocketFactory(sc.getSocketFactory());
      // Now you can access an https URL without having the certificate in
      // the truststore
      URL url = new URL(urlStr);
      URLConnection urlConn = url.openConnection();
      urlConn.setDoInput(true);
      urlConn.setUseCaches(false);
      urlConn.setRequestProperty("Content-Type", "text/xml");
      FileInputStream bf = new FileInputStream(file);
      // Send POST output.
      DataOutputStream printout = new DataOutputStream(urlConn
          .getOutputStream());
      byte[] buf = new byte[2048];
      int num = bf.read(buf);
      while (num != (-1))// 是否讀完文件
      {
        printout.write(buf, 0, num);// 把文件內容寫到網路緩衝區
        num = bf.read(buf);// 繼續從文件中讀取數據
      }
      printout.flush();// 把緩衝區的數據寫往客戶端
      bf.close();
      printout.close();
      BufferedReader input = null;
      try {
        input = new BufferedReader(new InputStreamReader(urlConn
            .getInputStream()));
      } catch (Exception e) {
        e.printStackTrace();
      }
      String str = input.readLine().trim();
      if (str.equals(strSuccessMessage)) {
        boolRetrun = true;
      } else {
        boolRetrun = false;
      }
    } catch (IOException ce) {
      if (TroubleDetector.DetectServer(this.url.getHost(), 80))
        ;
      throw new NetworkErrorException("網路發生問題!!", ce);
    } catch (Exception ce) {
      ce.printStackTrace();
      throw new NetworkErrorException("傳輸檔案發生問題!!", ce);
    }
    return boolRetrun;
  }


reply to postreply to post
話題樹型展開
人氣 標題 作者 字數 發文時間
15609 [精華] Tomcat SSL 問題 secretguest0824 1298 2006-12-22 14:50
13474 Re:Tomcat SSL 問題 secretguest0824 3255 2006-12-22 17:09
7042 Re:Tomcat SSL 問題 zrjian 89 2009-05-21 17:12
6976 Re:Tomcat SSL 問題 secretguest0824 201 2009-05-21 17:21
7022 Re:Tomcat SSL 問題 zrjian 439 2009-05-21 17:54
6988 Re:Tomcat SSL 問題 secretguest0824 763 2009-05-21 18:19
6954 Re:Tomcat SSL 問題 zrjian 664 2009-05-21 19:03
7064 Re:Tomcat SSL 問題 zrjian 168 2009-05-25 10:49
13792 Re:Tomcat SSL 問題 secretguest0824 1700 2006-12-25 14:34
13089 Re:Tomcat SSL 問題 secretguest0824 2543 2006-12-26 17:45
13001 Re:Tomcat SSL 問題 secretguest0824 69 2006-12-26 17:49
13055 Re:Tomcat SSL 問題 secretguest0824 109 2006-12-26 18:19
10651 Re:Tomcat SSL 問題 secretguest0824 227 2007-12-12 15:50
10892 Re:Tomcat SSL 問題 secretguest0824 713 2007-12-12 16:29
10680 Re:Tomcat SSL 問題 frankiakimo 46 2008-01-02 10:05
6546 Re:Tomcat SSL 問題 porsa 228 2009-07-29 17:54
6509 Re:Tomcat SSL 問題 tuu 375 2009-07-29 23:03
6512 Re:Tomcat SSL 問題 porsa 944 2009-07-30 09:09
7297 Re:Tomcat SSL 問題 tuu 331 2009-07-30 09:58
» JWorld@TW »  Application Server » Tomcat

reply to postflat modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8