Learning Poco: GET with HTTP
Poco is a modern, powerful open source C++ class libraries and frameworks
for building network- and internet-based applications that run on desktop, server and embedded systems.
It is well-documented but still lacks some tutorials. This article starts the series of tutorials
for learning Poco. In this article, we will GET some web resource with HTTP.
Before we start, you may have to download a copy of the complete edition of Poco libraries, then
make and make install it. After that, open up your favorite text editor,
copy the following lines, and save it to something like http_get.cc.
#include <Poco/Net/HTTPClientSession.h>
#include <Poco/Net/HTTPRequest.h>
#include <Poco/Net/HTTPResponse.h>
#include <Poco/StreamCopier.h>
#include <Poco/Path.h>
#include <Poco/URI.h>
#include <Poco/Exception.h>
#include <iostream>
#include <string>
using namespace Poco::Net;
using namespace Poco;
using namespace std;
int main(int argc, char **argv)
{
if (argc != 2)
{
cout << "Usage: " << argv[0] << " <uri>" << endl;
cout << " fetches the resource identified by <uri> and print it" << endl;
return -1;
}
try
{
// prepare session
URI uri(argv[1]);
HTTPClientSession session(uri.getHost(), uri.getPort());
// prepare path
string path(uri.getPathAndQuery());
if (path.empty()) path = "/";
// send request
HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
session.sendRequest(req);
// get response
HTTPResponse res;
cout << res.getStatus() << " " << res.getReason() << endl;
// print response
istream &is = session.receiveResponse(res);
StreamCopier::copyStream(is, cout);
}
catch (Exception &ex)
{
cerr << ex.displayText() << endl;
return -1;
}
return 0;
}
Then compile it with g++:
$ g++ -o http_get http_get.cc -lPocoNet
You have to add the -lPocoNet part, or you may get compile errors saying “undefined
reference blabla …” Now you can use it just in the terminal:
$ ./http_get
Usage: ./http_get <uri>
fetches the resource identified by <uri> and print it
$ ./http_get http://example.com
200 OK
Troubleshootings
If your compiler complains something like:
undefined reference to `Poco::URI::URI(char const*)'
undefined reference to `Poco::URI::getPort() const'
undefined reference to `Poco::URI::getPathAndQuery() const'
undefined reference to `Poco::Net::HTTPMessage::HTTP_1_1'
undefined reference to `Poco::Net::HTTPRequest::HTTP_GET'
undefined reference to `Poco::Net::HTTPClientSession::sendRequest(Poco::Net::HTTPRequest&)'
undefined reference to `Poco::Net::HTTPResponse::HTTPResponse()'
undefined reference to `Poco::Net::HTTPClientSession::receiveResponse(Poco::Net::HTTPResponse&)'
undefined reference to `Poco::Net::HTTPResponse::~HTTPResponse()'
undefined reference to `Poco::Net::HTTPRequest::~HTTPRequest()'
undefined reference to `Poco::Net::HTTPClientSession::~HTTPClientSession()'
undefined reference to `Poco::URI::~URI()'
undefined reference to `Poco::Exception::displayText() const'
Make sure you DO compile it with -lPocoNet:
$ g++ -o http_get http_get.cc -lPocoNet
If you see this:
error while loading shared libraries: libPocoNet.so.11: cannot open shared object file: No such file or directory
You may have to add /usr/local/lib to your ldconfig configuration.
Under Ubuntu 11.04, I resolve this by creating a new file /etc/ld.so.conf.d/poco.conf
as root, with the content saying /usr/local/lib. Then run /sbin/ldconfig
as root, and you will see the error is gone.
If you have any question, leave a message here and I will try my best to help.