【Java】通過httpClient同步非同步傳送請求
阿新 • • 發佈:2019-01-08
非同步和同步
概念:
同步:傳送一個請求,需要等待返回結果,然後才能傳送下一個請求
非同步:傳送一個請求,不需要等待返回結果,隨時可以傳送下一個請求
Java 同步 GET 方法
private void sendRequest() throws Exception{
String path ="/statistic/info";
CloseableHttpClient httpClient = HttpClients.createDefault();
// 建立一個 GET 請求
HttpGet httpGet = new HttpGet(path);
// 執行請求
CloseableHttpResponse response =httpClient.execute(httpGet);
//取響應的結果
int statusCode =response.getStatusLine().getStatusCode();
System.out.println(statusCode);
String content = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println(content);
//關閉httpclient
response.close();
httpClient.close();
}
Java Post方法
private void sendRequest() throws Exception{
String path ="http://localhost:8080/Baas/docs/index.html#!/device/payment.do";
CloseableHttpClient httpClient = HttpClients.createDefault();
// 建立一個 GET 請求
HttpGet httpPost = new HttpGet(path);
// 執行請求
CloseableHttpResponse response =httpClient.execute(httpPost);
//取響應的結果
String content = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println(content);
//關閉httpclient
response.close();
httpClient.close();
}
Java的非同步HttpClient
1 . Maven 設定
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpasyncclient</artifactId>
<version>4.1.1</version>
</dependency>
2 . demo 程式碼
A. 獲得 request
String url = "http://localhost:8080/Baas/device/carenter.do";
HttpPost httpPost = new HttpPost(url);
//裝填引數
List<NameValuePair> nvps = new ArrayList<>();
nvps.add(new BasicNameValuePair("third_serial_num", serialNum));
nvps.add(new BasicNameValuePair("pay_time", txTime));
nvps.add(new BasicNameValuePair("park_no", parkNo));
nvps.add(new BasicNameValuePair("plate_num", plate));
nvps.add(new BasicNameValuePair("park_start_time", startTime));
nvps.add(new BasicNameValuePair("park_end_time", endTime));
nvps.add(new BasicNameValuePair("pay_amount", amount));
nvps.add(new BasicNameValuePair("tempid", ""));
nvps.add(new BasicNameValuePair("sign", macSign));
//設定引數到請求物件中
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
return httpPost;
B. 非同步
// 傳入HttpPost request
CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
httpclient.start();
System.out.println(" caller thread id is : " + Thread.currentThread().getId());
httpclient.execute(request, new FutureCallback<HttpResponse>() {
@Override
public void completed(final HttpResponse response) {
System.out.println(" callback thread id is : " + Thread.currentThread().getId());
System.out.println(request.getRequestLine() + "->" + response.getStatusLine());
try {
String content = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println(" response content is : " + content);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void failed(final Exception ex) {
System.out.println(request.getRequestLine() + "->" + ex);
System.out.println(" callback thread id is : " + Thread.currentThread().getId());
}
@Override
public void cancelled() {
System.out.println(request.getRequestLine() + " cancelled");
System.out.println(" callback thread id is : " + Thread.currentThread().getId());
}
});