Specifying a Custom Message-Body Handler
By default,
RWHttpClient::getReply() returns an
RWHttpReply object. The body of the message is stored as an
RWCString internally and is accessible through the
RWHttpReply::getBody() member function. You can specify an alternate mechanism for reading the body of the message from the underlying data connection by passing an
RWTFunctor handler to the
RWHttpClient::getReply() function.
Example 12 creates a function that reads from a portal and writes the associated data to a file.
Example 12 – Creating a function that reads from a portal
void writeToFile(RWPortal portal, RWCString filename) {
RWStreamCoupler coupler(RWStreamCoupler::binary); // 1
ofstream ostrm(filename); // 2
RWPortalIStream istrm(portal); // 3
couple(istrm, ostrm); // 4
}
After writing a function that can read from a portal and write the data to a file, you can create an
RWTFunctor handler that uses the function to read the body of an HTTP reply, as shown in
Example 13 .
Example 13 – Using an RWTFunctor handler
RWHttpClient client = RWHttpSocketClient::make();
// initialize, connect, and submit a request…
RWTFunctor<void(RWPortal)> handler; // 1
handler = rwBind(writeToFile, rw1, "request.out"); // 2
RWHttpReply reply = client.getReply(handler); // 3
NOTE: RWHttpAgent does not include a mechanism for specifying a body handler. If the body of a message requires special treatment (for instance, it cannot be stored temporarily in an RWCString), then your application must use RWHttpClient to retrieve the document.