![]()
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:
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 |