攔截器(interceptor)是那些有助於阻止或改變請求或響應的攔截器。協定攔截器通常作用於特定檔頭或一組相關檔頭。HttpClient庫為攔截器提供支援。
HttpRequestInterceptor
介面表示請求攔截器。此介面包含一個稱為進程的方法,需要編寫程式碼塊來攔截請求。
在用戶端,此方法在將請求傳送到伺服器之前驗證/處理請求,並且在伺服器端,此方法在評估請求的主體之前執行。
可以按照以下步驟建立請求攔截器。
第1步 - 建立HttpRequestInterceptor
的物件
通過實現其抽象方法過程來建立HttpRequestInterceptor
介面的物件。
HttpRequestInterceptor requestInterceptor = new HttpRequestInterceptor() {
@Override
public void process(HttpRequest request, HttpContext context) throws
HttpException, IOException {
//Method implementation . . . . .
};
第2步 - 範例化CloseableHttpClient物件
通過將以上建立的攔截器新增到它來構建自定義的CloseableHttpClient
物件,如下所示 -
//Creating a CloseableHttpClient object
CloseableHttpClient httpclient =
HttpClients.custom().addInterceptorFirst(requestInterceptor).build();
使用此物件,可以照常執行請求執行。
以下範例演示了請求攔截器的用法。在此範例中,建立了一個HTTP GET請求物件,並新增了三個檔頭:sample-header
,demoheader
和test-header
。
在攔截器processor()
方法中,驗證傳送請求的頭部; 如果這些檔頭中的任何一個是sample-header
,我們嘗試刪除它並顯示特定請求的檔頭列表。
import java.io.IOException;
import org.apache.http.Header;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HttpContext;
public class InterceptorsExample {
public static void main(String args[]) throws Exception{
//Creating an HttpRequestInterceptor
HttpRequestInterceptor requestInterceptor = new HttpRequestInterceptor() {
@Override
public void process(HttpRequest request, HttpContext context) throws
HttpException, IOException {
if(request.containsHeader("sample-header")){
System.out.println("Contains header sample-header, removing it..");
request.removeHeaders("sample-header");
}
//Printing remaining list of headers
Header[] headers= request.getAllHeaders();
for (int i = 0; i<headers.length;i++){
System.out.println(headers[i].getName());
}
}
};
//Creating a CloseableHttpClient object
CloseableHttpClient httpclient =
HttpClients.custom().addInterceptorFirst(requestInterceptor).build();
//Creating a request object
HttpGet httpget1 = new HttpGet("https://www.kaops.com/");
//Setting the header to it
httpget1.setHeader(new BasicHeader("sample-header","My first header"));
httpget1.setHeader(new BasicHeader("demo-header","My second header"));
httpget1.setHeader(new BasicHeader("test-header","My third header"));
//Executing the request
HttpResponse httpresponse = httpclient.execute(httpget1);
//Printing the status line
System.out.println(httpresponse.getStatusLine());
}
}
執行上面範例程式碼,得到以下結果:
Contains header sample-header, removing it..
demo-header
test-header
HTTP/1.1 200 OK
HttpResponseInterceptor
介面表示響應攔截器。該介面包含一個稱為process()
的方法。在此方法中,需要編寫程式碼塊來攔截響應。
在伺服器端,此方法在將響應傳送到用戶端之前驗證/處理響應,在用戶端,此方法在評估響應主體之前執行。
建立響應攔截器
可以按照以下步驟建立響應攔截器 -
第1步 - 建立HttpResponseInterceptor的物件
通過實現其抽象方法過程來建立HttpResponseInterceptor
介面的物件。
HttpResponseInterceptor responseInterceptor = new HttpResponseInterceptor() {
@Override
public void process(HttpResponse response, HttpContext context) throws HttpException, IOException {
//Method implementation . . . . . . . .
}
};
第2步 - 範例化CloseableHttpClient物件
通過向其新增上面建立的攔截器來構建自定義的CloseableHttpClient
物件,如下所示 -
//Creating a CloseableHttpClient object
CloseableHttpClient httpclient =
HttpClients.custom().addInterceptorFirst(responseInterceptor).build();
使用此物件,可以照常執行請求執行。
以下範例演示了響應攔截器的用法。在此範例中,向處理器中的響應新增了三個檔頭:sample-header
,demo-header
和test-header
。
執行請求並獲得響應後,使用getAllHeaders()
方法列印響應的所有檔頭的名稱。
在輸出中,可以觀察列表中三個標題的名稱。
import java.io.IOException;
import org.apache.http.Header;
import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
import org.apache.http.HttpResponseInterceptor;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HttpContext;
public class ResponseInterceptorsExample {
public static void main(String args[]) throws Exception{
//Creating an HttpRequestInterceptor
HttpResponseInterceptor responseInterceptor = new HttpResponseInterceptor() {
@Override
public void process(HttpResponse response, HttpContext context) throws
HttpException, IOException {
System.out.println("Adding header sample_header, demo-header, test_header to the response");
response.setHeader("sample-header", "My first header");
response.setHeader("demo-header", "My second header");
response.setHeader("test-header", "My third header");
}
};
//Creating a CloseableHttpClient object
CloseableHttpClient httpclient = HttpClients.custom().addInterceptorFirst(responseInterceptor).build();
//Creating a request object
HttpGet httpget1 = new HttpGet("https://www.kaops.com/");
//Executing the request
HttpResponse httpresponse = httpclient.execute(httpget1);
//Printing remaining list of headers
Header[] headers = httpresponse.getAllHeaders();
for (int i = 0; i<headers.length;i++){
System.out.println(headers[i].getName());
}
}
}
執行上面範例程式碼,得到以下結果:
On executing the above program generates the following output.
Adding header sample_header, demo-header, test_header to the response
Accept-Ranges
Access-Control-Allow-Headers
Access-Control-Allow-Origin
Cache-Control
Content-Type
Date
Expires
Last-Modified
Server
Vary
X-Cache
sample-header
demo-header
test-header