HttpUrlConnection和HttpClient速度对比

本文聚焦于接口测试中用Java发送HTTP请求,对比了HttpUrlConnection和HttpClient框架的速度。通过代码测试,对百度首页进行一万次访问,结果显示HttpUrlConnection耗时66285ms,HttpClient耗时223342ms,前者耗时更短,且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.HttpClient;
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.CloseableHttpResponse;
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比较烦人的一点是打印信息太多了,不知道怎么去掉哈哈哈,还是得要多多的研究啊。

  

转载于:https://www.cnblogs.com/daliqinchuanniu/p/11039804.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值