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
發送請求主要分為以下幾步:
- 創建 CloseableHttpClient對象/CloseableHttpAsyncClient對象,前者同步,后者異步
- 創建具體的Http請求對象,例如HttpGet,HttpPost
- 調用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
發送請求主要分為以下幾步:
- 創建OkHttpClient對象
- 創建Request對象
- 將Request 對象封裝為Call
- 通過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 進行設置,無法對單個請求設置。