Click here to Skip to main content
Licence CC (ASA 3U)
First Posted 10 Sep 2011
Views 8,501
Bookmarked 4 times

Learning Poco: GET with HTTP

By | 10 Sep 2011 | Technical Blog
Learning Poco: GET with HTTP
A Technical Blog article. View original blog here.[^]

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 <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

About the Author

stfairy

Student
Shanghai Jiao Tong University
China China

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionLink to your others articles [modified] PinmemberSpatlabor14:02 12 Sep '11  
AnswerRe: Link to your others articles Pinmemberstfairy15:13 12 Sep '11  
QuestionCan you use it with Visual C++ PinmemberCaner Korkmaz4:14 10 Sep '11  
AnswerRe: Can you use it with Visual C++ Pinmemberstfairy6:00 10 Sep '11  
GeneralRe: Can you use it with Visual C++ PinmemberCaner Korkmaz8:23 10 Sep '11  

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 10 Sep 2011
Article Copyright 2011 by stfairy
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid