From: Mikko Rasa Date: Wed, 10 Aug 2011 18:44:40 +0000 (+0300) Subject: Add example of Http::Client usage X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=5b161886744ecbf3960ffe6ee5601df6d4a217ce;p=libs%2Fnet.git Add example of Http::Client usage --- diff --git a/.gitignore b/.gitignore index bee94e5..d16c915 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ /.options.* /.profile /debug +/httpget /libmspnet.a /libmspnet.so /mspnet.pc diff --git a/Build b/Build index 1ed9db8..47032c4 100644 --- a/Build +++ b/Build @@ -36,4 +36,13 @@ package "mspnet" library "mspnet"; }; }; + + program "httpget" + { + source "examples/httpget.cpp"; + build_info + { + library "mspnet"; + }; + }; }; diff --git a/examples/httpget.cpp b/examples/httpget.cpp new file mode 100644 index 0000000..70942b7 --- /dev/null +++ b/examples/httpget.cpp @@ -0,0 +1,58 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace Msp; + +class HttpGet: public RegisteredApplication +{ +private: + bool verbose; + string url; + +public: + HttpGet(int, char **); + + virtual int main(); +}; + +HttpGet::HttpGet(int argc, char **argv): + verbose(false) +{ + GetOpt getopt; + getopt.add_option('v', "verbose", verbose, GetOpt::NO_ARG); + getopt(argc, argv); + + const vector &args = getopt.get_args(); + if(args.empty()) + throw usage_error("No URL"); + + url = args.front(); +} + +int HttpGet::main() +{ + Http::Client client; + + if(verbose) + { + IO::print("=== Sending request ===\n"); + client.start_request(Http::Request::from_url(url)); + IO::print(client.get_request()->str()); + client.wait_response(); + const Http::Response *response = client.get_response(); + IO::print("=== Got response ===\n"); + IO::print(response->str()); + } + else + { + const Http::Response *response = client.get_url(url); + IO::print(response->get_content()); + } + + return 1; +}