1. 程式人生 > >請求攔截器HttpRequestInterceptor

請求攔截器HttpRequestInterceptor

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow

也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!

                public interface
HttpRequestInterceptor
org.apache.http.HttpRequestInterceptor
Known Indirect Subclasses
BasicHttpProcessor, HttpProcessor, RequestAddCookies, RequestConnControl, RequestContent, 
RequestDate, RequestDefaultHeaders, RequestExpectContinue, RequestProxyAuthentication, 
RequestTargetAuthentication, RequestTargetHost, RequestUserAgent

BasicHttpProcessor  Keeps lists of interceptors for processing requests and responses. 
HttpProcessor  Performs interceptor processing of requests and responses. 
RequestAddCookies  Request interceptor that matches cookies available in the current CookieStore to the request being executed and generates corresponding cookierequest headers. 
RequestConnControl
  A request interceptor that suggests connection keep-alive to the server. 
RequestContent  A request interceptor that decides about the transport encoding. 
RequestDate  A request interceptor that adds a Date header. 
RequestDefaultHeaders 
 Request interceptor that adds default request headers. 
RequestExpectContinue  A request interceptor that enables the expect-continue handshake. 
RequestProxyAuthentication   
RequestTargetAuthentication   
RequestTargetHost  A request interceptor that sets the Host header for HTTP/1.1 requests. 
RequestUserAgent  A request interceptor that adds a User-Agent header. 
Class Overview
Processes a request. Provides the ability to process a request before it is sent to the server 
or after it has received on the server side.

HttpRequestInterceptor就是Http請求攔截器。
可用在客服端,在Http訊息發出前,對HttpRequest  request做些處理。比如加頭啊
也可用在伺服器端,在Http到達後,正式處理前,對HttpRequest  request做些處理。

HttpRequestInterceptor聲明瞭一個方法(只有一個)以便處理HttpRequest  request。
Public Methods
public abstract void process (HttpRequest request, HttpContext context)
Since: API Level 1
Processes a request. On the client side, this step is performed before the request is sent to the server. On the server side, this step is performed on incoming messages before the message body is evaluated.
Parameters
request  the request to preprocess
context  the context for the request
Throws
IOException  in case of an IO problem
HttpException  in case of a protocol or other problem 

HttpRequestInterceptor 用在客服端時,在http訊息傳送到伺服器前,該方法被呼叫。
HttpRequestInterceptor 用在伺服器端時,在http訊息到達伺服器後,但在被系統處理前,該方法被呼叫。
示例1:
DefaultHttpClient client=new DefaultHttpClient();
.................................
if (sendHeaders.size()>0)
{
 HttpRequestInterceptor itcp=new HttpRequestInterceptor()
 {
  public void  process (HttpRequest request, HttpContext context)
 throws HttpException,IOException
 {
   for (String key:sendHeaders.keySet())
  {
    if (!request.containsHeader(key))
   {
    request.addHeader(key,sendHeaders.get(key));
   }
  }
 } 
 };
 client.addRequestInterceptor(itcp);
}
注意:RequestAddCookies, RequestConnControl, RequestContent, 
RequestDate, RequestDefaultHeaders, RequestExpectContinue, RequestProxyAuthentication, 
RequestTargetAuthentication, RequestTargetHost, RequestUserAgent
它們好像是對應Http頭的處理,但是它們的使用還不清楚。            

給我老師的人工智慧教程打call!http://blog.csdn.net/jiangjunshow

這裡寫圖片描述