HttpClient

class

A HTTP GET Request Client.

This class implements a HTTP GET Request, and provide you with a notification, when the response arrives.

You provide an URL and one or more callback functions, for getting the response data.

Example

HttpClient client("http://www.myip.com");
client.setDataReadyCallback<MyClass>(this, &MyClass::httpData);

Now your method httpData is called when response data arrives. Note that the callback can be called multiple times. A flag in the returned data object indicates, when this was the last data to be received.

DNS resolution is done automatically. You can also change the HTTP port to something other than 80 by providing an alternative port via the URL: . Likewise, you can use raw IPv4 addresses: .

Headers

You can provide additional headers for the request by using the constructors optional second argument. Just remember to end each header line with a . Like this: Accept: text/html\r\n

Error handling

This client class handles error that arise from:

  • DNS lookup failed (no such domain name)
  • No server response (host does not respond)

This class does not handle errors based on HTTP status codes or content types.

If you wish to handle errors you should setup a callback for the setErrorCallback

Public Functions

HttpClient::HttpClient()

Construct an empty invalid client object.

HttpClient::HttpClient(String anUrl, String headers)

Construct a new HTPT request to a given URL.

To receive the response, also setup the callback setDataReadyCallback

Parameters
  • anUrl -

    The URL string

  • headers -

    Any additional headers to add

template <typename Owner>
void mono::network::HttpClient::setDataReadyCallback(Owner * cnxt, void(Owner::*)(const HttpResponseData &) memPtr)

Provide your callback function to handle response data.

Parameters
  • cnxt -

    A pointer to the callback member function context (the this pointer)

  • memPtr -

    A pointer to the member function, that is the callback

void mono::network::HttpClient::setDataReadyCallback(void (*cfunc)(const HttpResponseData&))

Provide your callback function to handle response data.

Parameters
  • cfunc -

    A pointer to the function that handles the data response

Protected Functions

void HttpClient::dnsResolutionError(INetworkRequest::ErrorEvent *evnt)

Error callback for dns resolver

virtual void HttpClient::dnsComplete(INetworkRequest::CompletionEvent *evnt)

dns resolver completion event handler

void HttpClient::httpCompletion(redpine::ManagementFrame::FrameCompletionData *data)

when the HTTP GET frame completes, check for error

class

Class to represent HTTP response data.

This class defines the response data chunk return by an HTTP request. It defines only public accessable properties, that indicate the response state and the raw data.

Public Members

HttpClient *mono::network::HttpClient::HttpResponseData::Context

Pointer to the originating HttpClient object.

String mono::network::HttpClient::HttpResponseData::bodyChunk

The raw HTTP response chunk. More chunk might arrive later.

bool mono::network::HttpClient::HttpResponseData::Finished

true if this response chunk is the final.