把这个帖子作为日记,记下来,如果title带有为什么,Javaeye会自动把它移到问答频道,重新输入一下,看看高手们的反应:
最近为了查一个问题,分别使用HttpClient3和HttpClient4发一个同样post请求到同一个server上,发现 HttpClient3比HttpClient4性能要好很多,测试结果是H3,平均时间是800ms,而httpClient4要3秒钟,代码如下:
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.httpclient.DefaultMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
/*
* @author Tomy Zhou
* @since 1.0
* @version ; HttpClientTest.java 2010-7-6
**/
public class HttpClientTest {
public static void main(String[] args) throws Exception {
/***************************************
*
* Method1
*
* Using HttpClient3
*
*
*
*
*/
long startTime = System.currentTimeMillis();
NameValuePair[] postData = new NameValuePair[1];
postData[0] = new NameValuePair("hl", "zh-CN");
PostMethod post = new PostMethod("http://www.google.com.hk/search");
HttpClient client = new HttpClient();
int httpStatusCode = -1;
InputStream in = null;
// Set RetryHandler
DefaultMethodRetryHandler retryhandler = new DefaultMethodRetryHandler();
retryhandler.setRequestSentRetryEnabled(true);
retryhandler.setRetryCount(3);
post.setMethodRetryHandler(retryhandler);
// Set Post Data
post.setRequestBody(postData);
BufferedReader ins = null;
String loginResult = null;
// Execute Post Request
try {
// (client.getHttpConnectionManager()).closeIdleConnections(0);
httpStatusCode = client.executeMethod(post);
in = post.getResponseBodyAsStream();
ins = new BufferedReader(new InputStreamReader(in, "ISO-8859-1"));
String line = null;
StringBuffer outbuffer = new StringBuffer();
while ((line = ins.readLine()) != null) {
outbuffer.append(line);
}
loginResult = outbuffer.toString();
System.out.println(loginResult);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
in.close();
}
if (ins != null) {
ins.close();
}
if (post != null) {
post.releaseConnection();
// (client.getHttpConnectionManager()).closeIdleConnections(0);
}
}
System.out.println("The httpClient3 duration is ======"
+ (System.currentTimeMillis() - startTime));
/*******************************************************************
*
*
*Using HttpClient4 method1 to test
*
*
*/
startTime = System.currentTimeMillis();
DefaultHttpClient httpclient = new DefaultHttpClient();
// 目标地址
HttpPost httppost = new HttpPost("http://www.google.com.hk/search");
// 构造最简单的字符串数据
StringEntity reqEntity = new StringEntity("hl=zh-CN");
// 设置请求的数据
httppost.setEntity(reqEntity);
// 执行
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
// 显示结果
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(entity
.getContent(), "UTF-8"));
String line = null;
StringBuffer outbuffer = new StringBuffer();
while ((line = reader.readLine()) != null) {
outbuffer.append(line);
}
System.out.println(outbuffer);
} finally {
if (reader != null) {
reader.close();
}
}
httpclient.getConnectionManager().shutdown();
System.out.println("The httpClient4 duration is ======"
+ (System.currentTimeMillis() - startTime));
Thread.sleep(10000);
/*******************************************************************
*
*
*Using HttpClient4 method2 to test
*
*
*/
startTime = System.currentTimeMillis();
// 3.Use HttpClient4 - 2
DefaultHttpClient httpclient2 = new DefaultHttpClient();
// 目标地址
HttpPost httppost2 = new HttpPost("http://www.google.com.hk/search");
// 构造最简单的字符串数据
List<org.apache.http.NameValuePair> formParams = new ArrayList<org.apache.http.NameValuePair>();
formParams.add(new BasicNameValuePair("hl", "zh-CN"));
HttpEntity entityForm = new UrlEncodedFormEntity(formParams, "UTF-8");
// 设置请求的数据
httppost2.setEntity(entityForm);
// 执行
HttpResponse response2 = httpclient2.execute(httppost);
HttpEntity entity2 = response2.getEntity();
System.out.println("----------------------------------------");
// 显示结果
BufferedReader reader2 = new BufferedReader(new InputStreamReader(
entity2.getContent(), "UTF-8"));
String line2 = null;
StringBuffer outbuffer2 = new StringBuffer();
while ((line2 = reader2.readLine()) != null) {
outbuffer2.append(line2);
}
System.out.println(outbuffer2);
httpclient2.getConnectionManager().shutdown();
System.out.println("The httpClient4-2 duration is ======"
+ (System.currentTimeMillis() - startTime));
}
}
请大家赐教,什么原因造成这么大的差别。