Advertisement
Guest User

tspRocco

a guest
Mar 8th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.40 KB | None | 0 0
  1. #include <Poco/Net/HTTPClientSession.h>
  2. #include <Poco/Net/HTTPCredentials.h>
  3. #include <Poco/Net/HTTPRequest.h>
  4. #include <Poco/Net/HTTPResponse.h>
  5. #include <Poco/JSON/Object.h>
  6. #include <Poco/JSON/Stringifier.h>
  7. #include <Poco/StreamCopier.h>
  8. #include <Poco/Path.h>
  9. #include <Poco/URI.h>
  10. #include <Poco/Exception.h>
  11. #include <iostream>
  12. #include <string>
  13.  
  14.  
  15.  
  16. int main(int argc, char **argv)
  17. {
  18.     // The SW takes ONE arguments, a unique identifier (URI).
  19.     if (argc != 2)
  20.     {
  21.         std::cout << "Usage: " << argv[0] << " <uri>" << std::endl;
  22.         std::cout << "       fetches the resource identified by <uri> and print it" << std::endl;
  23.         return -1;
  24.     }
  25.  
  26.     try
  27.     {
  28.         // prepare session
  29.         Poco::URI uri("http://192.168.33.128:8080/api/authentication" /*argv[1]*/);
  30.         std::cout << "\nSending command with URI: " << uri.getHost() << ", port " << uri.getPort() << std::endl;
  31.         Poco::Net::HTTPClientSession session(uri.getHost(), uri.getPort());
  32.         session.setKeepAlive(true);
  33.         Poco::Net::HTTPCredentials creds("tsrf", "tsrf");
  34.        
  35.         // prepare path
  36.         std::string path(uri.getPathAndQuery());
  37.         if (path.empty()) path = "/";
  38.  
  39.         //// First creating a JSON:
  40.         //Poco::JSON::Object::Ptr root = new Poco::JSON::Object();
  41.  
  42.         //root->set("id", "42");
  43.         //root->set("value", "the_answer");
  44.  
  45.         //Poco::JSON::Object::Ptr subItem = new Poco::JSON::Object();
  46.         //root->set("elem", subItem);
  47.         //subItem->set("subval", "1");
  48.         //subItem->set("subval", "2");
  49.         //subItem->set("subval", "3");
  50.  
  51.         //std::ostringstream os;
  52.         //root->stringify(os, 2);
  53.         //std::string reqBody = os.str();
  54.  
  55.  
  56.  
  57.         //// login json JSON:
  58.         //Poco::JSON::Object::Ptr root = new Poco::JSON::Object();
  59.  
  60.         //root->set("j_username", "tsmf");
  61.         //root->set("j_password", "tsmf");
  62.         //root->set("remember-me", "true");
  63.  
  64.         //std::ostringstream os;
  65.         //root->stringify(os, 2);
  66.         //std::string reqBody = os.str();
  67.  
  68.         // Alternative solution:
  69.         std::string reqBody = "j_username=tsmf&j_password=tsmf&remember-me=true";
  70.  
  71.         // create request
  72.         Poco::Net::HTTPRequest req(Poco::Net::HTTPRequest::HTTP_POST, path, Poco::Net::HTTPMessage::HTTP_1_1);
  73.         req.setContentType("application/x-www-form-urlencoded");
  74.         //req.setContentType("multipart/form-data"); // requires a body.
  75.  
  76.         req.setKeepAlive(true);
  77.         req.setContentLength(reqBody.length());
  78.         std::cout << "\nSending request with body: " << reqBody << std::endl;
  79.         std::ostream& myOStream = session.sendRequest(req); // sends request, returns open stream
  80.         myOStream << reqBody;  // sends the body
  81.         req.write(std::cout);
  82.         //
  83.         //// send request
  84.         //session.sendRequest(req);
  85.  
  86.         // get response
  87.         Poco::Net::HTTPResponse res;
  88.         std::cout << "\nResponse received. " << std::endl;
  89.         std::istream &is = session.receiveResponse(res);
  90.  
  91.         std::cout << res.getStatus() << std::endl << res.getReason();
  92.         Poco::StreamCopier::copyStream(is, std::cout);
  93.  
  94.         // if error is 401, resending with aurhorization.
  95.         if (res.getStatus() == Poco::Net::HTTPResponse::HTTP_UNAUTHORIZED)
  96.         {
  97.             std::cout << "\nNot authorized. Autenticating..." << std::endl;
  98.             creds.authenticate(req, res);
  99.             session.sendRequest(req);
  100.  
  101.             // Receive the response again.
  102.             session.receiveResponse(res);
  103.             std::cout << res.getStatus() << std::endl << res.getReason();
  104.         }
  105.  
  106.     }
  107.     catch (Poco::Exception &ex)
  108.     {
  109.         std::cerr << ex.displayText() << std::endl;
  110.         std::cin.get();
  111.         return -1;
  112.     }
  113.  
  114.     std::cin.get();
  115.  
  116.     return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement