package com.gimslab.https_test;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.KeyStore;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;

public class HttpsHCApache {

    /**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
	 new HttpsHCApache().httpsGetTest();
    }

    private void httpsGetTest() throws Exception {
	DefaultHttpClient httpclient = new DefaultHttpClient();

	KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
	FileInputStream instream = new FileInputStream(new File("jssecacerts"));
	try {
	    trustStore.load(instream, "changeit".toCharArray());
	} finally {
	    instream.close();
	}

	SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
	Scheme sch = new Scheme("https", socketFactory, 443);
	httpclient.getConnectionManager().getSchemeRegistry().register(sch);

//	HttpGet httpget = new HttpGet("https://h.gimslab.com/");
	HttpGet httpget = new HttpGet("https://www.google.com/");

	System.out.println("executing request" + httpget.getRequestLine());

	HttpResponse response = httpclient.execute(httpget);
	HttpEntity entity = response.getEntity();

	System.out.println("----------------------------------------");
	System.out.println(response.getStatusLine());
	if (entity != null) {
	    System.out.println("Response content length: "
		    + entity.getContentLength());
	}

	InputStream is = null;
	try{
	    is = entity.getContent();
	    byte[] buff = new byte[512];
	    int read = -1;
	    while((read=is.read(buff, 0, buff.length))!=-1){
		System.out.print(new String(buff, 0, read));
	    }
	}
	finally{
	    is.close();
	}

	if (entity != null) {
	    entity.consumeContent();
	}

	httpclient.getConnectionManager().shutdown();
    }

}

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2010-07-08 19:26:21
Processing time 0.0053 sec