实例化一个org.apache.commons.httpclient.protocol.Protocol对象。创立这个实例时,需求一个正当的协定类型(如https),一个定制的socket factory,和一个默许的端中号(如https的443端口).
Protocol myhttps = new Protocol("https", new MySSLSocketFactory(), 443);
然后,这个实例可被设置为协定的处置器。
HttpClient httpclient = new HttpClient();
httpclient.getHostConfiguration().setHost("www.whatever.com", 443, myhttps);
GetMethod httpget = new GetMethod("/");
httpclient.executeMethod(httpget);
经由过程挪用Protocol.registerProtocol办法,将此定制的实例,注册为某一特定协定的默许的处置器。由此,可以很便利地定制本人的协定类型(如myhttps)。
Protocol.registerProtocol("myhttps",
new Protocol("https", new MySSLSocketFactory(), 9443));
...
HttpClient httpclient = new HttpClient();
GetMethod httpget = new GetMethod("myhttps://www.whatever.com/");
httpclient.executeMethod(httpget);
假如想用本人定制的处置器代替https默许的处置器,只需求将其注册为"https"便可。
Protocol.registerProtocol("https",
new Protocol("https", new MySSLSocketFactory(), 443));
HttpClient httpclient = new HttpClient();
GetMethod httpget = new GetMethod("https://www.whatever.com/");
httpclient.executeMethod(httpget);
public static final String TARGET_HTTPS_SERVER = "www.verisign.com";
public static final int TARGET_HTTPS_PORT = 443;
public static void main(String[] args) throws Exception {
Socket socket = SSLSocketFactory.getDefault().
createSocket(TARGET_HTTPS_SERVER, TARGET_HTTPS_PORT);
try {
Writer out = new OutputStreamWriter(
socket.getOutputStream(), "ISO-8859-1");
out.write("GET / HTTP/1.1rn");
out.write("Host: " + TARGET_HTTPS_SERVER + ":" +
TARGET_HTTPS_PORT + "rn");
out.write("Agent: SSL-TESTrn");
out.write("rn");
out.flush();
BufferedReader in = new BufferedReader(
new InputStreamReader(socket.getInputStream(), "ISO-8859-1"));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} finally {
socket.close();
}
}
}
11、httpclient的多线程处置
利用多线程的次要目标,是为了完成并行的下载。在httpclient运转的过程当中,每一个http协定的办法,利用一个HttpConnection实例。因为毗连是一种无限的资本,每一个毗连在某一时辰只能供一个线程和办法利用,所以需求确保在需求时准确地分派毗连。HttpClient采取了一品种似jdbc毗连池的办法来办理毗连,这个办理任务由 MultiThreadedHttpConnectionManager完成。
MultiThreadedHttpConnectionManager connectionManager =
new MultiThreadedHttpConnectionManager();
HttpClient client = new HttpClient(connectionManager);
此是,client可以在多个线程中被用来履行多个办法。每次挪用HttpClient.executeMethod() 办法,城市去链接收理器请求一个毗连实例,请求胜利这个链接实例被签出(checkout),随之在链接利用完后必需偿还办理器。办理器撑持两个设置: maxConnectionsPerHost 每一个主机的最大并行链接数,默许为2
maxTotalConnections 客户端总并行链接最大数,默许为20
办理重视新使用链接时,接纳早偿还者先重用的体例(least recently used approach)。
因为是利用HttpClient的法式而不是HttpClient自己来读取应对包的主体,所以HttpClient没法决意甚么工夫毗连不再利用了,这也就请求在读完应对包的主体后必需手工显式地挪用releaseConnection()来释放请求的链接。
MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
HttpClient client = new HttpClient(connectionManager);
...
// 在某个线程中。
GetMethod get = new GetMethod("http://jakarta.apache.org/");
try {
client.executeMethod(get);
// print response to stdout
System.out.println(get.getResponseBodyAsStream());
} finally {
// be sure the connection is released back to the connection
// manager
get.releaseConnection();
}
对每个HttpClient.executeMethod须有一个method.releaseConnection()与之婚配.
GetMethod get = new GetMethod("http://jakarta.apache.org");
// 履行办法,并处置掉败的恳求.
...
InputStream in = get.getResponseBodyAsStream();
// 使用输出流来处置信息。
get.releaseConnection();