0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

CPPRESTメモ

Posted at

クラウドに定周期で情報伝達を行うシステムに関わった。
その際、RESTやら認証やらいろいろあったので、作成期間もないことからCPPRESTを
使用することとした。

sample.cpp(VC2017)
......................................................
#include <cpprest/http_client.h>
#include <cpprest/json.h>
......................................................
using namespace web;
using namespace web::http;
using namespace web::http::client;
using namespace web::json;

起動停止ボタンでスレッドを起動してその中で送信を実施

UINT  MyThread(LPVOID p)
{
......................................................
     while (ptr->Thread_flag == TRUE)
	try {
		auto task = oauth(ptr->A, ptr->B, ptr->C);
        }
     }

上記のスレッドからタスクとして起動される。

pplx::task <bool> oauth(double A, int B, std::string C)
{
	return pplx::create_task([A, B, C]()
	{

		json::value postData;

		postData[L"ID"] = json::value::value(B);
		postData[L"controlPoint"][L"NAME1"] = json::value::string(U("SETPOINT"));
		postData[L"controlPoint"][L"value"] = json::value::number(A);
		postData[L"controlPoint"][L"unit"] = json::value::string(U("M"));


		// proxyあり
		//auto config = http_client_config();
		//config.set_validate_certificates(false);
		//config.set_timeout(std::chrono::seconds(time));
		//config.set_proxy(web_proxy(utility::conversions::to_string_t("http://XXX.XXX.XXX.XXX:YYY")));

		//credentials cred(utility::conversions::to_string_t("VVVVV"),utility::conversions::to_string_t("ZZZZ"));
		// Set credentials
		//config.set_credentials(cred);
		http_client client(L"https://ABC.com/INPUT", config);

		http_request request(methods::POST);
		request.headers().add(L"Content-Type", L"application/json");
		//std::string Bear = "Bearer ";
		//std::string Msg = Bear + C;
		//request.headers().add(L"Authorization", Msg.c_str());
		std::string Basic = "Basic " + C;
		request.headers().add(L"Authorization", Basic.c_str());
		request.set_body(postData.serialize());

		return client.request(request);
	}).then([](http_response response)
	{
		if (response.status_code() == status_codes::OK)
		{
			//auto body = response.extract_string();
			//std::wcout << body.get().c_str() << std::endl;
			//std::cout << response.extract_json() << std::endl;
			//return pplx::task_from_result(json::value());
			return response.extract_json(true);
		}
		else
		{
			// return an empty JSON value
			return pplx::task_from_result(json::value());
		}

	}).then([](json::value json)
	{
		// リザルトコードを返す
		bool res = FALSE;
//		try {
			res = json[L"returnResult"].as_bool();
/*		}
		catch (http_exception const & e)
		{
			res = FALSE;
		}
		catch (const std::exception& e)
		{
			res = FALSE;
		}
		*/
		return res;

	});
}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?