HttpClient,okhttp,Jodd-http 使用上的差异

http作为最常用的网络请求方式,用来交换数据,不同的http客户端,性能使用方式都有所差别,本文将对HttpClient,okhttp,Jodd-http三者的put,post请求方式做一个对比。


 1 <dependency> 2 <groupId>org.jodd</groupId> 3 <artifactId>jodd-http</artifactId> 4 <version>5.1.4</version> 5 </dependency> 6  7 <!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp --> 8 <dependency> 9 <groupId>com.squareup.okhttp3</groupId>10 <artifactId>okhttp</artifactId>11 <version>4.4.0</version>12 </dependency>13 14 <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->15 <dependency>16 <groupId>org.apache.httpcomponents</groupId>17 <artifactId>httpclient</artifactId>18 <version>4.5.12</version>19 </dependency>20 21 <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->22 <dependency>23 <groupId>com.alibaba</groupId>24 <artifactId>fastjson</artifactId>25 <version>1.2.62</version>26 </dependency>27 

所需依赖

Apache HttpClient

  发送请求主要分为以下几步:

  1. 创建 CloseableHttpClient对象/CloseableHttpAsyncClient对象,前者同步,后者异步
  2. 创建具体的Http请求对象,例如HttpGet,HttpPost
  3. 调用execute方法执行请求,如果是异步请求在执行之前需调用start方法

get请求

 @Test public void testApacheHttpGet(String url) throws IOException { //设置超时时间 RequestConfig config = RequestConfig.custom() .setConnectTimeout(60 * 1000) //连接超时时间 .setSocketTimeout(60 * 1000) //从服务器获取响应数据的超时时间 .build(); CloseableHttpClient client = HttpClientBuilder.create().build(); HttpGet httpGet = new HttpGet(url); httpGet.setConfig(config); CloseableHttpResponse response = client.execute(httpGet); System.out.println(EntityUtils.toString(response.getEntity())); }

post请求:

 @Test public void testApacheHttpPost(String url) throws IOException { CloseableHttpClient client = HttpClientBuilder.create().build(); HttpPost httpPost = new HttpPost(url); httpPost.setHeader("Content-Type", "application/json;charset=utf8"); httpPost.setEntity(new StringEntity(new JSONObject().toString())); //设置请求体 CloseableHttpResponse response = client.execute(httpPost); System.out.println(EntityUtils.toString(response.getEntity())); }

okhttp

  发送请求主要分为以下几步:

  1. 创建OkHttpClient对象
  2. 创建Request对象
  3. 将Request 对象封装为Call
  4. 通过Call 来执行同步或异步请求,调用execute方法同步执行,调用enqueue方法异步执行

get请求:

 @Test public void testOkHttpGet(String url) throws IOException { Request request = new Request.Builder() .url(url) .get() .build(); Call call = okHttpClient.newCall(request); Response response = call.execute(); System.out.println(response.body().toString()); }

post请求:

 @Test public void testOkHttpPost(String url) throws IOException { JSONObject json = new JSONObject(); RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),String.valueOf(json)); Request build = new Request.Builder() .url(url) .post(body) .header("Content-Type","application/json;charset=utf8") .build(); Call call = okHttpClient.newCall(build); Response response = call.execute(); String string = response.body().string(); }

设置超时

 OkHttpClient client = new OkHttpClient.Builder() .connectTimeout(60, TimeUnit.SECONDS)//设置连接超时时间 .readTimeout(60, TimeUnit.SECONDS)//设置读取超时时间 .build();

对client设置超时,意味着所有请求方式都将采取此种超时设置。

Jodd-http

  Jodd提供的一个轻量级、原生的的http客户端,使用起来很简单、方便。

get请求:

 请求参数可以直接拼接在url后面,也可以通过query()方法指定

 @Test public void testJoddHttpGet(String url) throws IOException { HttpResponse response = HttpRequest .get(url) //指定请求方式 .contentType("application/json") //指定编码方式 .query("xxx", "xxx") .connectionKeepAlive(true)//长连接  .timeout(60 * 1000) //超时设置 .send(); //发送请求 System.out.println(response.bodyText()); }

  response存储服务器返回的数据。可以从response实例中提取出各种属性,如statusCode()或者statusPhrase()。

  response读取响应body有三种方法:

 response.bodyText() body文本,以头信息指定的方式编码 response.bodyBytes() body字节 response.body()    以ISO-8859-1 encoding

post请求:

  请求体存放在bodyText中。也可以通过form("xx","xx")表单加参数

 public void testJoddHttpPost(String url) throws IOException { HttpResponse response = HttpRequest .post(url) .contentType("application/json") .bodyText("xxx") .send(); System.out.println(response.bodyText()); }

小结:

  • 代码量方面来看 jodd-http < okhttp <apache-http。
  • 超时设置上,jodd-http和apache-httpclient更为灵活,okhttp对client 进行设置,无法对单个请求设置。

相关文章