/* * SSLWebClient.java * * Created on den 10 januari 2005, 12:23 */ import ip1.u2.WebPane; import java.net.URL; import java.net.URLConnection; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLSession; import javax.net.ssl.HttpsURLConnection; import java.net.InetAddress; import java.io.*; import javax.swing.JTextArea; /** * * @author Henrik & Maria */ public class SSLWebClient extends WebPane implements HostnameVerifier { /** Creates a new instance of SSLWebClient */ public SSLWebClient() { } public boolean verify(String hostname, SSLSession session) { try { // Det här känns helgalet men funkar åtminstonne med exemplet i uppgiften String dn = session.getPeerCertificateChain()[0].getSubjectDN().getName(); int start = dn.indexOf("CN=") + 3; int end = dn.indexOf(",", start); String certname = dn.substring(start, end); InetAddress iaU = InetAddress.getByName(hostname); InetAddress iaC = InetAddress.getByName(certname); return iaU.equals(iaC); } catch(Exception e) { e.printStackTrace(); } return false; } protected void addContent(String urlString) { try { URL url = new URL(urlString); InputStream is; if(url.getProtocol().equals("https")) { HttpsURLConnection huc = (HttpsURLConnection) url.openConnection(); huc.setHostnameVerifier(this); is = huc.getInputStream(); } else { URLConnection uc = url.openConnection(); is = uc.getInputStream(); } BufferedReader br = new BufferedReader(new InputStreamReader(is)); String line = ""; content.setText(line); while((line = br.readLine()) != null) System.out.println(line); ((JTextArea) content).append(line + '\n'); } catch (IOException e) { e.printStackTrace(); content.setText(e.toString()); } } /** * @param args the command line arguments */ public static void main(String[] args) { if(args.length > 0) { SSLWebClient sslwc = new SSLWebClient(); sslwc.addContent(args[0]); } } }