1 19 20 package com.maverick.ssl; 21 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.io.OutputStream ; 25 import java.net.Socket ; 26 import java.net.URL ; 27 import java.net.URLConnection ; 28 import java.net.UnknownHostException ; 29 30 import com.maverick.crypto.asn1.x509.X509Certificate; 31 import com.maverick.ssl.https.HttpsURLStreamHandlerFactory; 32 33 37 public class SSLSocket extends Socket { 38 39 SSLTransport transport; 40 org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(SSLSocket.class); 42 43 45 public SSLSocket(String host, int port) throws IOException , UnknownHostException , SSLException { 46 this(host, port, false); 47 } 48 49 public SSLSocket(String host, int port, boolean delayHandshake) throws IOException , UnknownHostException , SSLException { 50 super(host, port); 51 transport = SSLTransportFactory.newInstance(); 52 if (!delayHandshake) 53 startHandshake(null); 54 } 55 56 public InputStream getInputStream() throws IOException { 57 return transport.getInputStream(); 58 } 59 60 public OutputStream getOutputStream() throws IOException { 61 return transport.getOutputStream(); 62 } 63 64 protected void startHandshake(SSLContext context) throws IOException , SSLException { 65 transport.initialize(super.getInputStream(), super.getOutputStream()); 66 } 67 68 protected InputStream getRawInputStream() throws IOException { 69 return super.getInputStream(); 70 } 71 72 protected OutputStream getRawOutputStream() throws IOException { 73 return super.getOutputStream(); 74 } 75 76 public static void main(String [] args) { 77 78 try { 79 80 HttpsURLStreamHandlerFactory.addHTTPSSupport(); 81 82 URL url = new URL ("https://3sp.com"); 84 URLConnection 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, */*"); con.setRequestProperty("Accept-Encoding", "gzip, deflate"); con.setRequestProperty("Accept-Language", "en-gb"); con.setRequestProperty("Connection", "Keep-Alive"); con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)"); 95 con.connect(); 96 97 InputStream 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 ex) { 107 ex.printStackTrace(); 108 } 109 110 } 111 } 112 | Popular Tags |