栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

java httpclient 慢

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1023830.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号