TOC PREV NEXT INDEX




HTTP Events


Events are fired when an HTTP request is prepared or sent and when an HTTP response is received. You can create a listener to receive these events, which contain the request and the response (if applicable), examine them, and even change certain aspects of the request and response. The listener can also cancel a request from being sent or cancel a response from being passed to the rendering engine.

The three kinds of events in the HTTP Event Notification API are as follows:

Event
Description
REQUEST_PREPARED
The HTTP request, represented by an HttpRequest object, has been prepared. In other words, the request line, the appropriate headers, and the optional entity body has been set and is ready to be sent. This event contains only a reference to the HttpRequest.
REQUEST_SENT
The HTTP request has been sent. This event contains only a reference to the HttpRequest.
RESPONSE_RECEIVED
The HTTP response has been received, and the status line, headers, and optional entity body have been parsed into an HttpResponse object. This event contains references to both the HttpRequest and the HttpResponse.

To create a listener for HTTP events, implement the HttpURLConnectionListener interface as in the following example:

public class MyHttpURLConnectionListener

implements HttpURLConnectionListener {

    public void onHttpURLConnectionEvent(

        HttpURLConnectionEvent event) {
 
        HttpRequest request;

        HttpResponse response;

        switch (event.getEventType()) {

            case HttpURLConnectionEvent.REQUEST_PREPARED :

                request = event.getRequest();

                /*

                * Investigate the HTTP request. It is possible

                * to cancel the request through event.consume().

                */

                break;

            case HttpURLConnectionEvent.REQUEST_SENT :

                request = event.getRequest();

                /*

                * The HTTP request has already been sent. In 

                * this case event.consume() had no effect!

                */

                break;

            case HttpURLConnectionEvent.RESPONSE_RECEIVED :

                request = event.getRequest();

                response = event.getResponse();

                /*

                * Investigate the HTTP request and response. It 

                * is possible to cancel the response through 

                * event.consume().

                */

                break;

        }

    }

}


 

Once the interface is implemented, you need to register the listener in the ConnectionManager, which maintains all the listeners, as in the following example:

ConnectionManager connectionManager = new ConnectionManager();

connectionManager.addHttpURLConnectionListener(

    new MyHttpURLConnectionListener());

HttpURLConnection.setGlobalConnectionManager(connectionManager);
 

To remove the listener, invoke the removeHttpURLConnectionListener( ) method.



Copyright 2005. ICEsoft Technologies, Inc.
http://www.icesoft.com

TOC PREV NEXT INDEX