コード例 #1
0
ファイル: main.cpp プロジェクト: vdvleon/myVideoCollection
		bool sendCommandJSON(const std::string & method, Poco::Dynamic::Var & output, const std::vector<Poco::Dynamic::Var> & params = std::vector<Poco::Dynamic::Var>())
		{
			// Create request
			Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, "/jsonrpc");
			
			// Authorization
			request.set("Authorization", "Basic bnpiZ2V0OnRlZ2J6bg==");
			
			// Create body
			Poco::Dynamic::Var body = Poco::Dynamic::Struct<std::string>();
			body["method"] = method;
			body["id"] = "sendCommandJSON";
			std::vector<Poco::Dynamic::Var> params_;
			params_.push_back("callback");
			for (std::vector<Poco::Dynamic::Var>::const_iterator it = params.begin(); it != params.end(); ++it)
			{
				params_.push_back(*it);
			}
			body["params"] = params_;
			std::string body_ = MyVideoCollection::JSON::stringify(body);
			request.setContentLength(body_.length());
			
			// Send request
			Poco::Net::HTTPClientSession session("127.0.0.1", port_);
			session.sendRequest(request) << body_ << std::flush;
			
			// Receive response
			Poco::Net::HTTPResponse response;
			std::istream & responseStream = session.receiveResponse(response);
			std::size_t bytes = response.getContentLength();
			std::stringstream response_;
			while (bytes > 0 && responseStream.good())
			{
				char buf[4096];
				responseStream.read(buf, std::min(bytes, (std::size_t)4096));
				std::size_t gcount = responseStream.gcount();
				bytes -= gcount;
				response_ << std::string(buf, gcount);
			}
			
			// Parse JSON
			output = MyVideoCollection::JSON::parse(response_);
			
			// Result?
			if (!output.isStruct())
			{
				output.empty();
				return false;
			}
			output = output["result"];
			
			return true;
		}