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...
}
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...
}
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.