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
所需依赖
发送请求主要分为以下几步:
@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())); }
@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())); }
发送请求主要分为以下几步:
@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()); }
@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客户端,使用起来很简单、方便。
请求参数可以直接拼接在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
请求体存放在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()); }