Java https 源码

  
    
1 /**
2 *
3 */
4 import java.io. * ;
5 import java.net. * ;
6 import java.security. * ;
7 import java.security.cert. * ;
8 import java.util. * ;
9 import javax.net.ssl. * ;
10 /**
11 * @author Administrator
12 *
13 */
14 public class HttpsTest {
15
16 /**
17 * @param args
18 */
19 public static void main(String[] args) {
20 // TODO Auto-generated method stub
21 HttpsTest httpsTest = new HttpsTest();
22
23 httpsTest.run();
24 }
25
26 // We would never hardcode this literal in a real system,
27 // this is only for this article.
28 private String url = " https://www.google.com/accounts/ServiceLogin?hl=zh-CN&continue=http://www.google.com.hk/ " ;
29
30 // Create an anonymous class to trust all certificates.
31 // This is bad style, you should create a separate class.
32 private X509TrustManager xtm = new X509TrustManager() {
33 public void checkClientTrusted(X509Certificate[] chain, String authType) {}
34
35 public void checkServerTrusted(X509Certificate[] chain, String authType) {
36
37 System.out.println( " ---cert: " + chain[ 0 ].toString() + " , 认证方式: " + authType);
38 }
39
40 public X509Certificate[] getAcceptedIssuers() {
41 return null ;
42 }
43 };
44
45 // Create an class to trust all hosts
46 private HostnameVerifier hnv = new HostnameVerifier() {
47 public boolean verify(String hostname, SSLSession session) {
48 System.out.println( " hostname:111111111111111111111 " + hostname);
49 return true ;
50 }
51 };
52
53 // In this function we configure our system with a less stringent
54 // hostname verifier and X509 trust manager. This code is
55 // executed once, and calls the static methods of HttpsURLConnection
56 public HttpsTest() {
57 // 初始化TLS协议SSLContext
58 // TrustManager
59 SSLContext sslContext = null ;
60
61 try {
62 sslContext = SSLContext.getInstance( " TLS " );
63 X509TrustManager[] xtmArray = new X509TrustManager[] { xtm };
64 sslContext.init( null , xtmArray, new java.security.SecureRandom());
65 } catch (GeneralSecurityException gse) {
66 // 打印出一些错误信息和处理这个异常
67
68 }
69
70 // 设置默认的SocketFactory和HostnameVerifier
71 // 为javax.net.ssl.HttpsURLConnection
72 if (sslContext != null ) {
73 HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
74 }
75
76 HttpsURLConnection.setDefaultHostnameVerifier(hnv);
77 }
78
79 // 这个函数被调用时,时而很重要的事
80 // 来这里需要注意的是,并没有特别的代码需要
81 // 被添加到一个“统一”处理URL。所有的信任
82 // 管理,验证,都是由HttpsURLConnection。
83 public void run() {
84 try {
85 URLConnection urlCon = ( new URL(url)).openConnection();
86 BufferedReader in = new BufferedReader( new InputStreamReader(urlCon.getInputStream()));
87 String line;
88
89 while ((line = in.readLine()) != null ) {
90 System.out.println( " Ambition-----> " + line);
91 }
92
93 // Whatever we want to do with these quotes
94 } catch (MalformedURLException mue) {
95 System.out.println( " MalformedURLException " );
96 mue.printStackTrace();
97 } catch (IOException ioe) {
98 System.out.println( " IOException " );
99 ioe.printStackTrace();
100 } catch (Exception e) {
101 System.out.println( " Exception " );
102 e.printStackTrace();
103 }
104 }
105
106 }

你可能感兴趣的:(https)