Cross-Platform C++

ot::web
class HttpClient

#include "ot/web/HttpClient.h"

A class for performing HTTP requests. The HttpClient class is the primary interface to a framework of classes which together provide an extensible and powerful implementation of the HTTP protocol.

The HttpClient class has been extensively improved in OpenTop 1.4 and now allows applications to:-

Applications that have no need to perform customized processing and simply need to read a resource from a HTTP server, may prefer to make use of the net::URLConnection class. This provides a simple, high-level abstraction of URL handling, but still employs the OpenTop HTTP framework under the covers.


Steps involved in performing an HTTP request

An application will typically perform the following steps when making an HTTP request:- Of course variations on the above scenario are possible. For example, it is not uncommon for an application to need to send an entity to a server before processing the result. The HttpRequest class allows the request to be modified in various ways: by specifying entity characteristics, timeout values, the HTTP method to be used and custom MIME header values.

The sending of entities is described in detail within the sendRequest() and HttpRequest documentation but, in summary, it can be performed synchronously as part of the request or asynchronously, with the application streaming the data to the server before using this class again to retrieve the final result.


HTTP Authorization

The HTTP protocol allows both proxy and origin servers to ask the client application for authorization tokens. This class handles authorization requests by delegating to the default net::Authenticator object (which the application can supply) and resending the original HTTP request with the appropriate authorization tokens supplied within the MIME headers. This behaviour is enabled by default, but may be controlled via the PerformAuthorization feature flag.


Customizing and enhancing HTTP request handling

The HttpClient class has the capability to handle most HTTP requests without the need for modification. However, if custom processing is required, applications can easily modify the behaviour to fit their needs.

The way HTTP requests are handled can be modified in two ways:-

  1. by enabling/disabling features
  2. by registering an appropriate request handler
When an application calls sendRequest(), a multi-stage process is started where each registered HttpRequestHandler (if any) is given the opportunity to modify or handle the request in a customized way. This process comprises of the following 5 stages:-
  1. Initialization/preparation, where HttpRequestHandler::newRequest() is called on each handler to prepare them for the request.
  2. Pre-processing, where HttpRequestHandler::handleRequest() is called on each handler to give them the opportunity to modify or handle the request entirely.
  3. If the request is to be sent over the network, The HttpClientConnectionManager is asked for a suitable connection to the remote host. The connection manager provides either a new connection or re-uses an idle persistent connection if one is available. The request is then written on the output stream. Assuming the application will not be streaming an entity following the request, the following stages are performed...
  4. The server's response is parsed into an HttpResponse object, and passed to each handler's HttpRequestHandler::handleResponseHeaders() method, where the handler has another opportunity to modify or replace the request with a new sub-request.
  5. Finally, if no more modifications to the request are needed, customized processing of the response body can be performed by the handlers' HttpRequestHandler::handleResponseBody() method.

An example

Before you delve into the details of this class, here is a simple example of a complete application which needs to POST a form field to an HTTP server and echo the resulting entity to stdout:-

    #include "ot/web/HttpRequest.h"
    #include "ot/web/HttpResponse.h"
    #include "ot/web/HttpClient.h"
    #include "ot/auxil/MemCheckSystemMonitor.h"

    #include <iostream>

    using namespace ot;
    using namespace ot::io;
    using namespace ot::web;
    using namespace std;

    void echoToStdout(InputStream* pStream)
    {
        Byte buffer[256];
        long bytesRead;
        while( (bytesRead=pStream->read(buffer, sizeof(buffer)))!=InputStream::EndOfFile)
            cout.write((char*)buffer, bytesRead);
    }

    int main(int argc, char* argv[])
    {
        auxil::MemCheckSystemMonitor monitor;
        try
        {
            URL url(OT_T("http://www.acme.org/search.php"));
            const char* search="q=OpenTop"; 
            HttpClient http;
            HttpRequest req(url);
            req.setConnectTimeout(5000); // timeout after 5 seconds 
            req.setEntity(HttpRequest::RawEntity, strlen(search), (const Byte*)search);
            req.getMimeHeaders()->setHeaderExclusive(OT_T("Content-Type"),OT_T("application/x-www-form-urlencoded"));
            
            HttpResponse resp = http.sendRequest(req);
            
            if(resp.getResponseCode() < 300)
            {
                echoToStdout(resp.getInputStream().get());
            }
        }
        catch(Exception& e)
        {
            cout << e.toString() << endl;
        }
        return 0;
    }

See also:
HttpRequest , HttpRequestHandler , HttpResponse , HttpClientConnection , HttpClientConnectionManager
Since:
OpenTop 1.4



Constructor/Destructor Summary
HttpClient()
         Default constructor.
HttpClient(HttpClientConnectionManager* pMgr)
         Constructor.

Method Summary
 RefPtr< HttpClientConnectionManager > getConnectionManager() const
         Returns the connection manager employed by this HttpClient instance to create and manage TCP/IP socket connections with HTTP servers.
 long getFeatureFlags() const
         Returns a bitfield containing all the features which are currently enabled for this HttpClient.
 HttpResponse getFinalResponse(HttpResponse& interimResponse)
         Called by the application to obtain the response to a HTTP request which included a streamed entity sent to the server.
 void registerHandler(HttpRequestHandler* pHandler)
         Registers an application-supplied HttpRequestHandler with this HttpClient.
 HttpResponse sendRequest(const HttpRequest& request)
         Sends a request to the HTTP server specified by the URL contained within the passed request.
 void setConnectionManager(HttpClientConnectionManager* pConnectionManager)
         Sets the connection manager to be employed by this HttpClient instance.
 void setFeatureFlags(int flags)
         Sets a bitfield representing all the features which are enabled for this HttpClient.
 void unregisterHandler(HttpRequestHandler* pHandler)
         Unregisters an application-supplied HttpRequestHandler from this HttpClient.

Enumerations

enum FeatureFlags { PerformAuthorization =0x01,   
  PerformRedirection =0x02,   
  PerformHeaderNormalization =0x04,   
  PerformTransferEncoding =0x10,   
  StandardFeatures =PerformAuthorization+PerformRedirection+PerformHeaderNormalization+PerformTransferEncoding}   


Constructor/Destructor Detail

HttpClient

 HttpClient()
Default constructor. The default constructor configures the HttpClient to support a set of standard features, and employs the global HttpClientConnectionManager for connection management.


HttpClient

 HttpClient(HttpClientConnectionManager* pMgr)
Constructor. This constructor configures the HttpClient to support a set of standard features, and employs the supplied HttpClientConnectionManager for connection management.

Parameters:
pMgr - a pointer to an instance of HttpClientConnectionManager which will be used to create and manage connections to HTTP servers.
Exceptions:
NullPointerException - if pMgr is null

Method Detail

getConnectionManager

RefPtr< HttpClientConnectionManagergetConnectionManager() const
Returns the connection manager employed by this HttpClient instance to create and manage TCP/IP socket connections with HTTP servers.


getFeatureFlags

long getFeatureFlags() const
Returns a bitfield containing all the features which are currently enabled for this HttpClient.


getFinalResponse

HttpResponse getFinalResponse(HttpResponse& interimResponse)
Called by the application to obtain the response to a HTTP request which included a streamed entity sent to the server. This method may only be called by passing an interim HttpResponse which resulted from a request that contained a streamed entity, and should be called when the application has finished streaming the entity to the remote server.

Exceptions:
IOException - if an I/O error occurs reading the response.
InterruptedIOException - if a socket timeout occurs.
IllegalArgumentException - if the interimResponse argument is not an interim response. This can be determined by first calling HttpResponse::isRequestInProgress().
See also:
HttpRequest::setTimeout()

registerHandler

void registerHandler(HttpRequestHandler* pHandler)
Registers an application-supplied HttpRequestHandler with this HttpClient.

Parameters:
pHandler - a pointer to the handler to register. This class will store the pointer within a RefPtr<> list, thereby increasing the reference-count for the duration that it is required.
Exceptions:
NullPointerException - if pHandler is null.

sendRequest

HttpResponse sendRequest(const HttpRequest& request)
Sends a request to the HTTP server specified by the URL contained within the passed request. See steps involved in sending a HTTP request for a detailed description of the process involved.

Parameters:
request - the HTTP request.
Returns:
either an interim or a final response. An interim response is returned if the request contains a streamed entity which the caller must write to the OutputStream contained in the returned HttpResponse; otherwise a final response is returned.
Exceptions:
IOException - if an error occurs processing the request.
InterruptedIOException - if either of the timeout values contained within the request triggers a network timeout.
Note:
Exceptions are thrown if I/O errors occur, but errors returned from the HTTP server can only be determined by interrogating the response code within the returned HttpResponse.

setConnectionManager

void setConnectionManager(HttpClientConnectionManager* pConnectionManager)
Sets the connection manager to be employed by this HttpClient instance.

Parameters:
pConnectionManager - a pointer to the HttpClientConnectionManager instance.
Exceptions:
NullPointerException - if pConnectionManager is null.

setFeatureFlags

void setFeatureFlags(int flags)
Sets a bitfield representing all the features which are enabled for this HttpClient. All the available features are described by bit values in the HttpClient::FeatureFlags enumeration. If a bit in the bitfield is switched on (a value of 1), then the corresponding feature is enabled, otherwise it is disabled and the logic associated with it will not be performed.


unregisterHandler

void unregisterHandler(HttpRequestHandler* pHandler)
Unregisters an application-supplied HttpRequestHandler from this HttpClient.

Parameters:
pHandler - a pointer to the handler to unregister.
Exceptions:
NullPointerException - if pHandler is null.


Cross-Platform C++

Found a bug or missing feature? Please email us at support@elcel.com

Copyright © 2000-2005 ElCel Technology   Trademark Acknowledgements