请求拦截器HttpRequestInterceptor

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头的处理,但是它们的使用还不清楚。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值