hutool HttpRequest VS httpClient 到底哪个快

 hutool HttpRequest VS httpClient 到底哪个快

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.StopWatch;

import cn.hutool.http.HttpRequest;

/**
 * httpClient VS hutool HttpRequest
 * 
 * @author douzi
 * @date 2024-05-17
 */
public class Test2 {
	public static void main(String[] args) {
		System.out.println("httpClient VS hutool HttpRequest:");
		StopWatch sw = new StopWatch();
		sw.start();

		String body = "";
		for(int i = 0; i < 1000; i ++) {
			body = HttpRequest.get("http://www.baidu.com")
					.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
					.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).timeout(3000)// 超时,毫秒
					.execute().body();
		}
		
		sw.stop();
		System.out.println("HttpRequest耗时:" + sw.getTotalTimeSeconds());
		
		StopWatch sw1 = new StopWatch();
		sw1.start();
		
		for(int i = 0; i < 1000; i ++) {
			HttpClient httpClient = new HttpClient();
	        HttpConnectionManagerParams managerParams = httpClient.getHttpConnectionManager().getParams();
			managerParams.setConnectionTimeout(3000); // 设置连接超时时间(单位毫秒)
			managerParams.setSoTimeout(3000 * 2); // 设置读数据超时时间(单位毫秒)
			
	        GetMethod method = new GetMethod("http://www.baidu.com");
	        method.addRequestHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
	        try {
	        	int statusCode = httpClient.executeMethod(method);
	            if (statusCode == HttpStatus.SC_OK) {
	    			method.getResponseBodyAsString();
	    		}
	        } catch(Exception e) {
	        } finally {
	        	method.releaseConnection();
	        }
		}
        
        sw1.stop();
		System.out.println("httpClient耗时:" + sw1.getTotalTimeSeconds());
	}
}

hutool HttpRequest VS httpClient(1000次调用相同请求)测试结果:

HttpRequest耗时:14.6822173
httpClient耗时:24.140532

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

窦再兴

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值