| 1 | // |
| 2 | // httpget.cpp |
| 3 | // |
| 4 | // This sample demonstrates the HTTPClientSession and the HTTPCredentials classes. |
| 5 | // |
| 6 | // Copyright (c) 2005-2012, Applied Informatics Software Engineering GmbH. |
| 7 | // and Contributors. |
| 8 | // |
| 9 | // SPDX-License-Identifier: BSL-1.0 |
| 10 | // |
| 11 | |
| 12 | |
| 13 | #include "Poco/Net/HTTPClientSession.h" |
| 14 | #include "Poco/Net/HTTPRequest.h" |
| 15 | #include "Poco/Net/HTTPResponse.h" |
| 16 | #include <Poco/Net/HTTPCredentials.h> |
| 17 | #include "Poco/StreamCopier.h" |
| 18 | #include "Poco/NullStream.h" |
| 19 | #include "Poco/Path.h" |
| 20 | #include "Poco/URI.h" |
| 21 | #include "Poco/Exception.h" |
| 22 | #include <iostream> |
| 23 | |
| 24 | |
| 25 | using Poco::Net::HTTPClientSession; |
| 26 | using Poco::Net::HTTPRequest; |
| 27 | using Poco::Net::HTTPResponse; |
| 28 | using Poco::Net::HTTPMessage; |
| 29 | using Poco::StreamCopier; |
| 30 | using Poco::Path; |
| 31 | using Poco::URI; |
| 32 | using Poco::Exception; |
| 33 | |
| 34 | |
| 35 | bool doRequest(Poco::Net::HTTPClientSession& session, Poco::Net::HTTPRequest& request, Poco::Net::HTTPResponse& response) |
| 36 | { |
| 37 | session.sendRequest(request); |
| 38 | std::istream& rs = session.receiveResponse(response); |
| 39 | std::cout << response.getStatus() << " " << response.getReason() << std::endl; |
| 40 | if (response.getStatus() != Poco::Net::HTTPResponse::HTTP_UNAUTHORIZED) |
| 41 | { |
| 42 | StreamCopier::copyStream(rs, std::cout); |
| 43 | return true; |
| 44 | } |
| 45 | else |
| 46 | { |
| 47 | Poco::NullOutputStream null; |
| 48 | StreamCopier::copyStream(rs, null); |
| 49 | return false; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | |
| 54 | int main(int argc, char** argv) |
| 55 | { |
| 56 | if (argc != 2) |
| 57 | { |
| 58 | Path p(argv[0]); |
| 59 | std::cout << "usage: " << p.getBaseName() << " <uri>" << std::endl; |
| 60 | std::cout << " fetches the resource identified by <uri> and print it to the standard output" << std::endl; |
| 61 | return 1; |
| 62 | } |
| 63 | |
| 64 | try |
| 65 | { |
| 66 | URI uri(argv[1]); |
| 67 | std::string path(uri.getPathAndQuery()); |
| 68 | if (path.empty()) path = "/" ; |
| 69 | |
| 70 | std::string username; |
| 71 | std::string password; |
| 72 | Poco::Net::HTTPCredentials::extractCredentials(uri, username, password); |
| 73 | Poco::Net::HTTPCredentials credentials(username, password); |
| 74 | |
| 75 | HTTPClientSession session(uri.getHost(), uri.getPort()); |
| 76 | HTTPRequest request(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1); |
| 77 | HTTPResponse response; |
| 78 | if (!doRequest(session, request, response)) |
| 79 | { |
| 80 | credentials.authenticate(request, response); |
| 81 | if (!doRequest(session, request, response)) |
| 82 | { |
| 83 | std::cerr << "Invalid username or password" << std::endl; |
| 84 | return 1; |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | catch (Exception& exc) |
| 89 | { |
| 90 | std::cerr << exc.displayText() << std::endl; |
| 91 | return 1; |
| 92 | } |
| 93 | return 0; |
| 94 | } |
| 95 | |