Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Poco/Net/HTTPClientSession.h>
- #include <Poco/Net/HTTPCredentials.h>
- #include <Poco/Net/HTTPRequest.h>
- #include <Poco/Net/HTTPResponse.h>
- #include <Poco/JSON/Object.h>
- #include <Poco/JSON/Stringifier.h>
- #include <Poco/StreamCopier.h>
- #include <Poco/Path.h>
- #include <Poco/URI.h>
- #include <Poco/Exception.h>
- #include <iostream>
- #include <string>
- int main(int argc, char **argv)
- {
- // The SW takes ONE arguments, a unique identifier (URI).
- if (argc != 2)
- {
- std::cout << "Usage: " << argv[0] << " <uri>" << std::endl;
- std::cout << " fetches the resource identified by <uri> and print it" << std::endl;
- return -1;
- }
- try
- {
- // prepare session
- Poco::URI uri("http://192.168.33.128:8080/api/authentication" /*argv[1]*/);
- std::cout << "\nSending command with URI: " << uri.getHost() << ", port " << uri.getPort() << std::endl;
- Poco::Net::HTTPClientSession session(uri.getHost(), uri.getPort());
- session.setKeepAlive(true);
- Poco::Net::HTTPCredentials creds("tsrf", "tsrf");
- // prepare path
- std::string path(uri.getPathAndQuery());
- if (path.empty()) path = "/";
- //// First creating a JSON:
- //Poco::JSON::Object::Ptr root = new Poco::JSON::Object();
- //root->set("id", "42");
- //root->set("value", "the_answer");
- //Poco::JSON::Object::Ptr subItem = new Poco::JSON::Object();
- //root->set("elem", subItem);
- //subItem->set("subval", "1");
- //subItem->set("subval", "2");
- //subItem->set("subval", "3");
- //std::ostringstream os;
- //root->stringify(os, 2);
- //std::string reqBody = os.str();
- //// login json JSON:
- //Poco::JSON::Object::Ptr root = new Poco::JSON::Object();
- //root->set("j_username", "tsmf");
- //root->set("j_password", "tsmf");
- //root->set("remember-me", "true");
- //std::ostringstream os;
- //root->stringify(os, 2);
- //std::string reqBody = os.str();
- // Alternative solution:
- std::string reqBody = "j_username=tsmf&j_password=tsmf&remember-me=true";
- // create request
- Poco::Net::HTTPRequest req(Poco::Net::HTTPRequest::HTTP_POST, path, Poco::Net::HTTPMessage::HTTP_1_1);
- req.setContentType("application/x-www-form-urlencoded");
- //req.setContentType("multipart/form-data"); // requires a body.
- req.setKeepAlive(true);
- req.setContentLength(reqBody.length());
- std::cout << "\nSending request with body: " << reqBody << std::endl;
- std::ostream& myOStream = session.sendRequest(req); // sends request, returns open stream
- myOStream << reqBody; // sends the body
- req.write(std::cout);
- //
- //// send request
- //session.sendRequest(req);
- // get response
- Poco::Net::HTTPResponse res;
- std::cout << "\nResponse received. " << std::endl;
- std::istream &is = session.receiveResponse(res);
- std::cout << res.getStatus() << std::endl << res.getReason();
- Poco::StreamCopier::copyStream(is, std::cout);
- // if error is 401, resending with aurhorization.
- if (res.getStatus() == Poco::Net::HTTPResponse::HTTP_UNAUTHORIZED)
- {
- std::cout << "\nNot authorized. Autenticating..." << std::endl;
- creds.authenticate(req, res);
- session.sendRequest(req);
- // Receive the response again.
- session.receiveResponse(res);
- std::cout << res.getStatus() << std::endl << res.getReason();
- }
- }
- catch (Poco::Exception &ex)
- {
- std::cerr << ex.displayText() << std::endl;
- std::cin.get();
- return -1;
- }
- std::cin.get();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement