Bringing it All Together
Example 9 is a complete document request using an RWHttpClient.
Example 9 – Requesting a document using an RWHttpClient
RWHttpClient client; // 1
RWHttpRequest request(RWHttpRequest::Get, "/"); // 2
client = RWHttpSocketClient::make(); // 3
try {
client.connect("www.perforce.com"); // 4
client.submit(request); // 5
RWHttpReply reply = client.getReply(); // 6
 
cout << reply.asString() << endl; // 7
 
if(reply.is2XX()) { // 8
cout << reply.getBody() << endl; // 9
}
} catch(const RWxmsg& msg) { // 10
// error...
}
//1 Creates an uninitialized RWHttpClient. The client object needs to be initialized before it can be used.
//2 Constructs an RWHttpRequest object to get the root document on the server.
//3 Initializes the RWHttpClient as an RWHttpSocketClient. The RWHttpSocketClient uses an RWSocketPortal to communicate with the server.
//4 Attempts to connect the RWHttpClient to an HTTP server running on www.perforce.com. The client assumes that the server is running on port 80.
//5 Submits a request to the HTTP server associated with the RWHttpClient. If problems with the RWHttpRequest are detected, the client does not send any data to the server and instead throws an exception describing the problem.
//6 Retrieves the HTTP response from the client and stores it in an RWHttpReply object. This function blocks until a complete reply is read from the server.
//7 Prints the basic information associated with the server's reply. This function formats and prints the status code and status message, the HTTP version of the response, and any headers that were associated with the reply.
//8 Checks the status of the RWHttpReply object, and ensures that it was successful (in the 200 range).
//9 Retrieves the body of the message from the reply. The body is generally the HTML document stored at the location specified, but any type of data may be returned by the server and stored in this string.
//10 Catches any errors that are thrown while attempting to process the HTTP request.
Example 10 shows the same request to an HTTP server using RWHttpAgent. It is a simple HTTP request-response interaction using only a single HTTP method (GET).
Example 10 – Requesting a document using an RWHttpAgent
RWHttpAgent agent; // 1
RWURL url("http://www.perforce.com/"); // 2
RWTIOUResult<RWHttpReply> replyIOU; // 3
 
try {
replyIOU = agent.executeGet(url); // 4
RWHttpReply reply = replyIOU.redeem(); // 5
 
cout << reply.asString() << endl;
 
if (reply.is2XX()) {
cout << reply.getBody() << endl;
}
} catch(const RWxmsg& msg) {
// error...
}
//1 Constructs an RWHttpAgent. No initialization is necessary for this class.
//2 Constructs an RWURL for the root document on the www.perforce.com server.
//3 Constructs an RWTIOUResult<> object to hold the return value from the execute command.
//4 Executes a GET request on the document located at url.
//5 Redeems the RWTIOUResult<> for an RWHttpReply object. This method blocks until the request executed in //4 completes.
NOTE: For more advanced examples, see the examples in the next section and the sample programs distributed with the HTTP package. Sample programs are located in the examples directory created for your installation. For more information, see the books Installing and Building Your SourcePro Products and Building Your Applications.