Results 1 to 2 of 2
  1. #1
    MCJP is offline Member
    Join Date
    Sep 2008
    Posts
    1
    Rep Power
    0

    Default HTTPS using certificate

    Good morning.

    I am fairly new to Java, so please be patient.

    I am currently working on a small Java class to recover the header of a URL to determine the last modification date. I am using a previously developed Java class that does it for HTTP, but want to extend it to HTTPS. I know that in general certificates should be imported into a keystore file so that secure sockets can refer to them. However, I would like to be able to load the certificate from a file dynamically.

    The original HTTP code is the following (using JDK 1.5)

    Java Code:
     
    package com.xxx.utils.http;
     
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.net.URLConnection;
     
    
    public class HttpFile {
    	private String url = null;
    	private HttpURLConnection urlConn = null;
     
    	public HttpFile(String url) {
    		this.url = url;
    		if (this.url != null) {
    			try {
    				URL u = new URL(url);
    				URLConnection conn = u.openConnection();
    				this.urlConn = (HttpURLConnection)conn;
    			}
    			catch (Exception ex) {
    				ex.printStackTrace();
    			}
    		}
    	}
    
    	public void Finalize() {
    		if (this.urlConn != null) {
    			this.urlConn.disconnect();
    		}
    		this.urlConn = null;
    	}
    
    	public boolean exists() {
    		try {
    			if (this.urlConn != null) {
    				return (this.urlConn.getResponseCode() == HttpURLConnection.HTTP_OK);
    			} else {
    				return false;
    			}
    		}
    		catch (Exception ex) {
    			return false;
    		}
    	}
    
    	public long lastModified() {
    		if (this.exists()) {
    			return this.urlConn.getHeaderFieldDate("Last-Modified", 0);
    		} else {
    			return 0L;
    		}
    	}
     
    
    	public static boolean exists(String url) {
    		HttpFile hf = new HttpFile(url);
    		boolean exists = hf.exists();
    		hf.Finalize();
    		return exists;
    	}
     
    
    	public static long lastModified(String url) {
    		HttpFile hf = new HttpFile(url);
    		long lastModified = hf.lastModified();
    		hf.Finalize();
    		return lastModified;
    	}
    }
    If you have any ideas, I would certainly appreciate it.
    Last edited by MCJP; 09-19-2008 at 04:52 PM.

  2. #2
    suprabha is offline Member
    Join Date
    Jul 2008
    Posts
    20
    Rep Power
    0

Similar Threads

  1. JDK 1.3 with HTTPS
    By sriram in forum Advanced Java
    Replies: 2
    Last Post: 01-21-2008, 11:51 PM
  2. Java certificate
    By Nick15 in forum Java Certification
    Replies: 2
    Last Post: 11-12-2007, 07:02 AM
  3. j2me.storage.RandomAccessStream certificate error
    By asdfghjkl in forum CLDC and MIDP
    Replies: 1
    Last Post: 11-09-2007, 04:27 PM
  4. How do i read from a website with a certificate??
    By karl in forum Advanced Java
    Replies: 0
    Last Post: 07-12-2007, 05:25 PM
  5. Replies: 0
    Last Post: 07-10-2007, 10:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •