接口测试中要用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比较烦人的一点是打印信息太多了,不知道怎么去掉哈哈哈,还是得要多多的研究啊。

  

posted on 2019-06-17 15:11  大荔秦川牛  阅读(1316)  评论(0编辑  收藏  举报