Click here to Skip to main content
15,868,070 members
Articles / Programming Languages / C++

Learning Poco: GET with HTTP

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
10 Sep 2011CC (ASA 3U)1 min read 64.3K   8   8
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 http_get.cc.

C++
#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

Troubleshooting

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 questions, leave a message here and I will try my best to help.

License

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


Written By
Student Shanghai Jiao Tong University
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionUndefined reference to symbol Pin
bordax11-Jul-15 15:09
bordax11-Jul-15 15:09 
AnswerRe: Undefined reference to symbol Pin
Member 1337166611-Oct-17 15:20
Member 1337166611-Oct-17 15:20 
QuestionLink to your others articles [modified] Pin
Spatlabor12-Sep-11 14:02
Spatlabor12-Sep-11 14:02 
AnswerRe: Link to your others articles Pin
stfairy12-Sep-11 15:13
stfairy12-Sep-11 15:13 
QuestionCan you use it with Visual C++ Pin
Caner Korkmaz10-Sep-11 4:14
Caner Korkmaz10-Sep-11 4:14 
AnswerRe: Can you use it with Visual C++ Pin
stfairy10-Sep-11 6:00
stfairy10-Sep-11 6:00 
GeneralRe: Can you use it with Visual C++ Pin
Caner Korkmaz10-Sep-11 8:23
Caner Korkmaz10-Sep-11 8:23 
GeneralRe: Can you use it with Visual C++ Pin
Joe_G25-Sep-13 3:36
Joe_G25-Sep-13 3:36 

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.