HttpUrlConnection和HttpClient速度對比
介面測試中要用java傳送http請求,看到常用的框架有HttpUrlConnection和HttpClient,所以寫了一小段程式碼對比一下速度:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class TestURLConnection {
static String link = "http://www.baidu.com";
public static void main(String[] args) {
long a = System.currentTimeMillis();
boolean flag = false;
for (int i = 0; i < 10000; ++i) {
if (flag) {
useURLConnection();
} else {
useHttpClient2();
}
System.out.println();
System.out.println();
}
long b = System.currentTimeMillis();
System.out.println("use time: " + (b - a) + " ms");
}
public static void useURLConnection() {
HttpURLConnection conn = null;
String result = "";
try {
URL url = new URL(link);
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(10000);
conn.connect();
InputStream urlStream = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(urlStream));
String s = "";
while ((s = reader.readLine()) != null) {
result += s;
}
reader.close();
urlStream.close();
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void useHttpClient() {
HttpClient client = new HttpClient();
GetMethod method = new GetMethod(link);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
try {
int statusCode = client.executeMethod(method);
String rspstr = method.getResponseBodyAsString();
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
method.releaseConnection();
}
}
public static void useHttpClient2(){
CloseableHttpClient client = HttpClients.createDefault();
HttpGet get = new HttpGet(link);
CloseableHttpResponse response = null;
try {
response = client.execute(get);
HttpEntity entity = response.getEntity();
String rspstr = EntityUtils.toString(entity);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
測試結果:
訪問百度首頁,訪問次數一萬次,耗時時間對比:
HttpUrlConnection:use time: 66285 ms
HttpClient: use time: 223342 ms
還是HttpUrlConnection耗時時間短點,而且HttpClient比較煩人的一點是列印資訊太多了,不知道怎麼去掉哈哈哈,還是得要多多的研究啊。