Using RWHttpAgent to Retrieve a Document Securely
This example uses the HTTP agent class on https:// URIs. An agent class uses its respective client class to perform common protocol requests.
This example uses utility functions provided in util.h.
Example 10 – Using a Secure Agent
// File: examples\https\manual\RWHttpSecureAgentExample.cpp
#include <rw/itc/RWTIOUResult.h>
#include <rw/internet/RWURL.h>
#include <rw/network/RWWinSockInfo.h>
#include <rw/secsock/RWSecureSocketPackageInit.h>
#include <rw/secsock/RWSecureSocketContext.h>
#include <rw/http/RWHttpAgent.h>
#include <rw/http/RWHttpReply.h>
#include <rw/https/RWHttpsSecurityManager.h>
#include <iostream>
using std::cout;
using std::cerr;
using std::endl;
using std::flush;
#include "args.h"
// This data is used to seed the RNG
const unsigned char seed[] = "123456789012345678901234567890"
"123456789012345678901234567890"
"123456789012345678901234567890"
"123456789012345678901234567890"
"123456789012345678901234567890";
int main(int argc, char** argv)
{
RWWinSockInfo info; //1
RWSecureSocketPackageInit secsockInit; //2
RWSecureSocketPackageInit::seedRNGFromMemory(seed,sizeof(seed)) //3
RWTValSlist<RWCString> args = processArgs(argc, argv);
if (args.entries() < 1)
{
cout << “Usage: “ << argv[0] << “ <URL>” << endl;
return 1;
}
RWURL (url(args[0]); //4
RWSecureSocketContext context; //5
// context.prepareToAuthenticate("<certificate file>"); //6
RWHttpsSecurityManager::setAgentContext(context); //7
RWHttpAgent agent; //8
RWTIOUResult<RWHttpReply> replyIOU; //9
try {
replyIOU = agent.executeGet(url); //10
RWHttpReply reply = replyIOU.redeem(); //11
cout << reply.asString() << endl;
if(reply.is2XX()) {
cout << reply.getBody() << endl; //12
}
} catch(const RWxmsg& msg) {
cerr << "An unexpected exception occurred: "
<< msg.why() << endl;
return 1;
}
return 0;
}