Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Learning Poco: GET with HTTP

, 10 Sep 2011
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 http_get.cc.

#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>
</span>
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

Share

About the Author

stfairy
Student Shanghai Jiao Tong University
China China
No Biography provided

Comments and Discussions

 
QuestionCan you use it with Visual C++ PinmemberCaner Korkmaz10-Sep-11 4:14 
Can you use it with Visual C++ ?
If can't -> can you use it with G++ in Windows ?
(Good Article Smile | :) )
AnswerRe: Can you use it with Visual C++ Pinmemberstfairy10-Sep-11 6:00 
GeneralRe: Can you use it with Visual C++ PinmemberCaner Korkmaz10-Sep-11 8:23 
GeneralRe: Can you use it with Visual C++ PinmemberJoe_G25-Sep-13 3:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web03 | 2.8.140922.1 | Last Updated 10 Sep 2011
Article Copyright 2011 by stfairy
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid