X-Git-Url: http://git.tdb.fi/?p=libs%2Fnet.git;a=blobdiff_plain;f=examples%2Fhttpget.cpp;fp=examples%2Fhttpget.cpp;h=70942b7647ffb0bcbdff535e902af0305eadb2ba;hp=0000000000000000000000000000000000000000;hb=5b161886744ecbf3960ffe6ee5601df6d4a217ce;hpb=b884825f8415c3bd300c433e1e155d98dbc56550 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; +}