Click here to Skip to main content
12,547,501 members (43,200 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


8 bookmarked

Learning Poco: GET with HTTP

, 10 Sep 2011 CC (ASA 3U)
Rate this:
Please Sign up or sign in to vote.
Learning Poco: GET with HTTP

Poco is a modern, powerful open source C++ class library and framework 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

#include <span class="code-keyword"><Poco/Net/HTTPClientSession.h>
</span>#include <span class="code-keyword"><Poco/Net/HTTPRequest.h>
</span>#include <span class="code-keyword"><Poco/Net/HTTPResponse.h>
</span>#include <span class="code-keyword"><Poco/StreamCopier.h>
</span>#include <span class="code-keyword"><Poco/Path.h>
</span>#include <span class="code-keyword"><Poco/URI.h>
</span>#include <span class="code-keyword"><Poco/Exception.h>
</span>#include <span class="code-keyword"><iostream>
</span>#include <span class="code-keyword"><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;

    // 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);

    // 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 -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
200 OK


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::
undefined reference to `Poco::Net::HTTPResponse::HTTPResponse()'
undefined reference to `Poco::Net::HTTPClientSession::
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 -lPocoNet

If you see this:

error while loading shared libraries: 
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/ 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 questions, leave a message here and I will try my best to help.


This article, along with any associated source code and files, is licensed under The Creative Commons Attribution-Share Alike 3.0 Unported License


About the Author

Student Shanghai Jiao Tong University
China China
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionLink to your others articles [modified] Pin
Spatlabor12-Sep-11 14:02
memberSpatlabor12-Sep-11 14:02 
AnswerRe: Link to your others articles Pin
stfairy12-Sep-11 15:13
memberstfairy12-Sep-11 15:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161018.1 | Last Updated 10 Sep 2011
Article Copyright 2011 by stfairy
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid