Using RWHttpsSecureSocketClient to Retrieve a Document
This example uses the HTTPS client class. A client class allows low-level access to and encapsulates the details of the protocol.
This example uses utility functions provided in util.h.
Example 11 – Using a Secure Client
// File: examples\https\manual\RWHttpSecureClientExample.cpp
#include <rw/network/RWWinSockInfo.h>
#include <rw/network/RWInetAddr.h>
#include <rw/secsock/RWSecureSocketPackageInit.h>
#include <rw/secsock/RWSecureSocketContext.h>
#include <rw/http/RWHttpClient.h>
#include <rw/http/RWHttpRequest.h>
#include <rw/https/RWHttpsSecureSocketClient.h>
#include <rw/internet/RWURL.h>
#include <iostream>
using std::cout;
using std::cerr;
using std::endl;
#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_DATA_FILE); //3
RWTValSlist<RWCString> args = processArgs(argc, argv);
if (args.entries() < 1)
{
cout << "Usage: " << argv[0] << " <url>" << endl;
return 1;
}
RWURL url(args[0]);
RWCString host = url.getHost();
RWCString portStr = url.getPort();
int port = atoi(portStr.data());
RWSecureSocketContext context; //4
// context.prepareToAuthenticate("<certificate file>"); //5
RWHttpClient client; //6
client = RWHttpsSecureSocketClient::make(context); //7
RWHttpRequest request(RWHttpRequest::Get, "/"); //8
try {
client.connect(host, port); //9
client.submit(request);
RWHttpReply reply = client.getReply(); //10
cout << reply.asString() << endl;
if(reply.is2XX()) {
cout << reply.getBody() << endl; //11
}
} catch(const RWxmsg& msg) {
cerr << "An unexpected exception occurred: "
<< msg.why() << endl;
return 1;
}
return 0;
}