HTTP Client是一個客戶端HTTP協議的類庫
1、首先是下載APACHE HTTP CLIENT相關的JAR,目前我引入到工程中的相關文件時:
httpclient-4.0.3.jar
httpcore-4.0.1.jar
httpmime-4.0.3.jar
commons-codec-1.4.jar
commons-logging-1.1.1.jar
JAR包之前的引用關系在README等相關說明中已有。
2、下載并運行TOMCAT,我下載的是TOMCAT6(對應的是JDK5),主要注意的是需要配置CATALINA_HOME這個環境變量。
3、制作服務器端證書,下面是我一個示例:
C:\Documents and Settings\dingjunxing>keytool -genkey -alias tomcat3 -keystore F
:\eclipse\workspace\httpc\tomcat3.keystore
輸入keystore密碼: 123456
您的名字與姓氏是什么?
[Unknown]: localhost
您的組織單位名稱是什么?
[Unknown]: sz
您的組織名稱是什么?
[Unknown]: sz
您所在的城市或區域名稱是什么?
[Unknown]: shenzhen
您所在的州或省份名稱是什么?
[Unknown]: guangdong
該單位的兩字母國家代碼是什么
[Unknown]: cn
CN=localhost, OU=sz, O=sz, L=shenzhen, ST=guangdong, C=cn 正確嗎?
[否]: y
輸入<tomcat3>的主密碼
(如果和 keystore 密碼相同,按回車): 123456
4、根據服務器證書導出客戶端證書,有兩種方式,一種為采用IE,下面提供一個利用KEY TOOL導出的方式:
keytool -export -file tomcat3.cert -alias tomcat3 -keystore F:\eclipse\workspace\httpc\tomcat3.keystore
注意:上面命令行必須進入到%java_home%/jre/lib/security中。
5、將獲取到的客戶端證書導入:
C:\Program Files\Java\jdk1.5.0_15\jre\lib\security>keytool -import -keystore cac
erts -file F:\eclipse\workspace\httpc\tomcat3.cert
輸入keystore密碼: 123456
Owner: CN=localhost, OU=sz, O=sz, L=shenzhen, ST=guangdong, C=cn
發照者: CN=localhost, OU=sz, O=sz, L=shenzhen, ST=guangdong, C=cn
序號: 4cc55438
有效期間: Mon Oct 25 17:56:08 CST 2010 至: Sun Jan 23 17:56:08 CST 2011
認證指紋:
MD5: E4:2E:BE:AC:A1:5D:E0:95:C7:95:93:BF:B3:F3:EE:5E
SHA1: F8:9A:BB:FA:C8:C5:8A:D2:FA:98:A1:95:64:65:42:9A:8F:0B:4A:7D
信任這個認證? [否]: y
認證已添加至keystore中
6、TOMCAT中相關設置,主要是修改CATALINA_HOME下的conf/server.xml,在其中添加如下一個連接器:
<Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS" keystoreFile="F:\eclipse\workspace\httpc\tomcat3.keystore" keystorePass="123456"/>
7、運行相關代碼:
package org.apache.http.examples.client;
import java.io.File;
import java.io.FileInputStream;
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;
/**
* This example demonstrates how to create secure connections with a custom SSL
* context.
*/
public class ClientCustomSSL {
public static void main(String[] args) throws Exception {
DefaultHttpClient httpclient = new DefaultHttpClient();
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
FileInputStream instream = new FileInputStream(new File("tomcat3.keystore"));
try {
trustStore.load(instream, "123456".toCharArray());
} finally {
instream.close();
}
SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
Scheme sch = new Scheme("https", socketFactory, 8443);
httpclient.getConnectionManager().getSchemeRegistry().register(sch);
HttpGet httpget = new HttpGet("https://localhost:8443/docs");
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());
}
if (entity != null) {
entity.consumeContent();
}
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
}
運行結果正常,當然也可以通過在瀏覽器中輸入“https://localhost:8443/”的方式進行訪問。