Dependents:   TimeZoneDemo EthernetJackTestCode MMEx_Challenge ntp_mem ... more

Embed: (wiki syntax)

« Back to documentation index

HTTPClient Class Reference

A simple HTTP Client. More...

#include <HTTPClient.h>

Inherits NetService.

Public Member Functions

 HTTPClient ()
 Instantiates the HTTP client.
void basicAuth (const char *user, const char *password)
 Provides a basic authentification feature (Base64 encoded username and password)
HTTPResult get (const char *uri, HTTPData *pDataIn)
 Executes a GET Request (blocking)
HTTPResult get (const char *uri, HTTPData *pDataIn, void(*pMethod)(HTTPResult))
 Executes a GET Request (non blocking)
template<class T >
HTTPResult get (const char *uri, HTTPData *pDataIn, T *pItem, void(T::*pMethod)(HTTPResult))
 Executes a GET Request (non blocking)
HTTPResult post (const char *uri, const HTTPData &dataOut, HTTPData *pDataIn)
 Executes a POST Request (blocking)
HTTPResult post (const char *uri, const HTTPData &dataOut, HTTPData *pDataIn, void(*pMethod)(HTTPResult))
 Executes a POST Request (non blocking)
template<class T >
HTTPResult post (const char *uri, const HTTPData &dataOut, HTTPData *pDataIn, T *pItem, void(T::*pMethod)(HTTPResult))
 Executes a POST Request (non blocking)
void doGet (const char *uri, HTTPData *pDataIn)
 Executes a GET Request (non blocking)
void doPost (const char *uri, const HTTPData &dataOut, HTTPData *pDataIn)
 Executes a POST Request (non blocking)
void setOnResult (void(*pMethod)(HTTPResult))
 Setups the result callback.
void setTimeout (int ms)
 Setups timeout.
virtual void poll ()
 This method can be inherited so that it is called on each Net::poll() call.
int getHTTPResponseCode ()
 Gets last request's HTTP response code.
void setRequestHeader (const string &header, const string &value)
 Sets a specific request header.
string & getResponseHeader (const string &header)
 Gets a response header.
void resetRequestHeaders ()
 Clears request headers.

Protected Member Functions

void close ()
 This flags the service as to be destructed if owned by the pool.

Detailed Description

A simple HTTP Client.

The HTTPClient is composed of:

  • The actual client (HTTPClient)
  • Classes that act as a data repository, each of which deriving from the HTTPData class (HTTPText for short text content, HTTPFile for file I/O, HTTPMap for key/value pairs, and HTTPStream for streaming purposes)

Definition at line 68 of file HTTPClient.h.


Constructor & Destructor Documentation

HTTPClient (  )

Instantiates the HTTP client.

Definition at line 37 of file HTTPClient.cpp.


Member Function Documentation

void basicAuth ( const char *  user,
const char *  password 
)

Provides a basic authentification feature (Base64 encoded username and password)

Definition at line 54 of file HTTPClient.cpp.

void close (  ) [protected]

This flags the service as to be destructed if owned by the pool.

Reimplemented from NetService.

Definition at line 214 of file HTTPClient.cpp.

void doGet ( const char *  uri,
HTTPData *  pDataIn 
)

Executes a GET Request (non blocking)

Executes a GET request on the URI uri

Parameters:
uri: URI on which to execute the request
pDataIn: pointer to an HTTPData instance that will collect the data returned by the request, can be NULL The function returns immediately and calls the previously set callback on completion or error

Definition at line 117 of file HTTPClient.cpp.

void doPost ( const char *  uri,
const HTTPData &  dataOut,
HTTPData *  pDataIn 
)

Executes a POST Request (non blocking)

Executes a POST request on the URI uri

Parameters:
uri: URI on which to execute the request
dataOut: a HTTPData instance that contains the data that will be posted
pDataIn: pointer to an HTTPData instance that will collect the data returned by the request, can be NULL
pMethod: callback function The function returns immediately and calls the previously set callback on completion or error

Definition at line 123 of file HTTPClient.cpp.

HTTPResult get ( const char *  uri,
HTTPData *  pDataIn,
T *  pItem,
void(T::*)(HTTPResult pMethod 
)

Executes a GET Request (non blocking)

Executes a GET request on the URI uri

Parameters:
uri: URI on which to execute the request
pDataIn: pointer to an HTTPData instance that will collect the data returned by the request, can be NULL
pItem: instance of class on which to execute the callback method
pMethod: callback method The function returns immediately and calls the callback on completion or error

Definition at line 108 of file HTTPClient.h.

HTTPResult get ( const char *  uri,
HTTPData *  pDataIn 
)

Executes a GET Request (blocking)

Executes a GET request on the URI uri

Parameters:
uri: URI on which to execute the request
pDataIn: pointer to an HTTPData instance that will collect the data returned by the request, can be NULL Blocks until completion

Definition at line 71 of file HTTPClient.cpp.

HTTPResult get ( const char *  uri,
HTTPData *  pDataIn,
void(*)(HTTPResult pMethod 
)

Executes a GET Request (non blocking)

Executes a GET request on the URI uri

Parameters:
uri: URI on which to execute the request
pDataIn: pointer to an HTTPData instance that will collect the data returned by the request, can be NULL
pMethod: callback function The function returns immediately and calls the callback on completion or error

Definition at line 77 of file HTTPClient.cpp.

int getHTTPResponseCode (  )

Gets last request's HTTP response code.

Returns:
The HTTP response code of the last request

Definition at line 176 of file HTTPClient.cpp.

string & getResponseHeader ( const string &  header )

Gets a response header.

Definition at line 186 of file HTTPClient.cpp.

void poll (  ) [virtual]

This method can be inherited so that it is called on each Net::poll() call.

Reimplemented from NetService.

Definition at line 152 of file HTTPClient.cpp.

HTTPResult post ( const char *  uri,
const HTTPData &  dataOut,
HTTPData *  pDataIn,
void(*)(HTTPResult pMethod 
)

Executes a POST Request (non blocking)

Executes a POST request on the URI uri

Parameters:
uri: URI on which to execute the request
dataOut: a HTTPData instance that contains the data that will be posted
pDataIn: pointer to an HTTPData instance that will collect the data returned by the request, can be NULL
pMethod: callback function The function returns immediately and calls the callback on completion or error

Definition at line 100 of file HTTPClient.cpp.

HTTPResult post ( const char *  uri,
const HTTPData &  dataOut,
HTTPData *  pDataIn 
)

Executes a POST Request (blocking)

Executes a POST request on the URI uri

Parameters:
uri: URI on which to execute the request
dataOut: a HTTPData instance that contains the data that will be posted
pDataIn: pointer to an HTTPData instance that will collect the data returned by the request, can be NULL Blocks until completion

Definition at line 94 of file HTTPClient.cpp.

HTTPResult post ( const char *  uri,
const HTTPData &  dataOut,
HTTPData *  pDataIn,
T *  pItem,
void(T::*)(HTTPResult pMethod 
)

Executes a POST Request (non blocking)

Executes a POST request on the URI uri

Parameters:
uri: URI on which to execute the request
dataOut: a HTTPData instance that contains the data that will be posted
pDataIn: pointer to an HTTPData instance that will collect the data returned by the request, can be NULL
pItem: instance of class on which to execute the callback method
pMethod: callback method The function returns immediately and calls the callback on completion or error

Definition at line 147 of file HTTPClient.h.

void resetRequestHeaders (  )

Clears request headers.

Definition at line 191 of file HTTPClient.cpp.

void setOnResult ( void(*)(HTTPResult pMethod )

Setups the result callback.

Parameters:
pMethod: callback function

Definition at line 129 of file HTTPClient.cpp.

void setRequestHeader ( const string &  header,
const string &  value 
)

Sets a specific request header.

Definition at line 181 of file HTTPClient.cpp.

void setTimeout ( int  ms )

Setups timeout.

Parameters:
ms: time of connection inactivity in ms after which the request should timeout

Definition at line 146 of file HTTPClient.cpp.