KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > ssl > SSLSocket


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.maverick.ssl;
21
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.net.Socket JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.net.URLConnection JavaDoc;
28 import java.net.UnknownHostException JavaDoc;
29
30 import com.maverick.crypto.asn1.x509.X509Certificate;
31 import com.maverick.ssl.https.HttpsURLStreamHandlerFactory;
32
33 /**
34  *
35  * @author Lee David Painter <a HREF="mailto:lee@3sp.com">&lt;lee@3sp.com&gt;</a>
36  */

37 public class SSLSocket extends Socket JavaDoc {
38
39     SSLTransport transport;
40     // #ifdef DEBUG
41
org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(SSLSocket.class);
42
43     // #endif
44

45     public SSLSocket(String JavaDoc host, int port) throws IOException JavaDoc, UnknownHostException JavaDoc, SSLException {
46         this(host, port, false);
47     }
48
49     public SSLSocket(String JavaDoc host, int port, boolean delayHandshake) throws IOException JavaDoc, UnknownHostException JavaDoc, SSLException {
50         super(host, port);
51         transport = SSLTransportFactory.newInstance();
52         if (!delayHandshake)
53             startHandshake(null);
54     }
55
56     public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
57         return transport.getInputStream();
58     }
59     
60     public OutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
61         return transport.getOutputStream();
62     }
63
64     protected void startHandshake(SSLContext context) throws IOException JavaDoc, SSLException {
65         transport.initialize(super.getInputStream(), super.getOutputStream());
66     }
67
68     protected InputStream JavaDoc getRawInputStream() throws IOException JavaDoc {
69         return super.getInputStream();
70     }
71
72     protected OutputStream JavaDoc getRawOutputStream() throws IOException JavaDoc {
73         return super.getOutputStream();
74     }
75
76     public static void main(String JavaDoc[] args) {
77
78         try {
79
80             HttpsURLStreamHandlerFactory.addHTTPSSupport();
81
82             URL JavaDoc url = new URL JavaDoc("https://3sp.com"); //$NON-NLS-1$
83

84             URLConnection JavaDoc con = url.openConnection();
85             con.setDoInput(true);
86             con.setDoOutput(false);
87             con.setAllowUserInteraction(false);
88
89             con.setRequestProperty("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*"); //$NON-NLS-1$ //$NON-NLS-2$
90
con.setRequestProperty("Accept-Encoding", "gzip, deflate"); //$NON-NLS-1$ //$NON-NLS-2$
91
con.setRequestProperty("Accept-Language", "en-gb"); //$NON-NLS-1$ //$NON-NLS-2$
92
con.setRequestProperty("Connection", "Keep-Alive"); //$NON-NLS-1$ //$NON-NLS-2$
93
con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)"); //$NON-NLS-1$ //$NON-NLS-2$
94

95             con.connect();
96
97             InputStream JavaDoc in = con.getInputStream();
98             int read;
99
100             while ((read = in.read()) > -1) {
101                 System.out.write(read);
102             }
103
104         } catch (SSLIOException ex) {
105             ex.getRealException().printStackTrace();
106         } catch (IOException JavaDoc ex) {
107             ex.printStackTrace();
108         }
109
110     }
111 }
112
Popular Tags