- C++ RESK SDK in Visual Studio 2013 설치 및 offline용 package 파일 (nupkg) 만들기
- Web Page 의 결과를 std::wstring 로 받아오는 방법 ( C++ REST SDK 사용 )
- Web Service의 json 결과를 받는 방법 ( C++ REST SDK 사용 )
- web::json::value 사용법 (C++ REST SDK)
안녕하세요. C++ RESK SDK (코드명 Casablanca)를 이용하여 Web Service 의 json 결과를 받는 방법에 대해서 설명드리겠습니다.
본 예제에 사용한 Web Service는 Grails 를 이용하여 작성하였습니다.
해당 내용에 대해서는 추후 Posting 할 예정이며, 제가 참조한 방법은 다음 Link에 있습니다.
https://jolorenz.wordpress.com/2014/02/28/create-a-restservice-api-with-grails-2-3-x-in-15-minutes/
저번 Posting 중 Web Page의 결과를 string으로 받아오는 방법에 대한 것이 있는데,
거기서 사용한 Source 를 활용하여 작성하였습니다.
http://devluna.blogspot.kr/2015/05/web-page-stdwstring-c-rest-sdk.html
그 결과로 받아온 json 파일은 다음과 같습니다.
[{"class":"com.jolorenz.rest.City","id":1,"cityName":"Munich","countryCode":"DE","dateCreated":"2015-05-19T04:59:07Z","lastUpdated":"2015-05-19T04:59:07Z","postalCode":"81927"},
{"class":"com.jolorenz.rest.City","id":2,"cityName":"Berlin","countryCode":"DE","dateCreated":"2015-05-19T04:59:07Z","lastUpdated":"2015-05-19T04:59:07Z","postalCode":"10115"}]
|
방법은 2가지로 했습니다.
1. 저번 Posting에서의 방법대로 먼저 std::wstring 로 가져와서 json 으로 parsing 하는 방법이 있습니다.
2. Web 에서 가져온 결과 자체를 json 으로 추출하는 방법이 있습니다.
아래 예제를 보시면 2가지 방법을 모두 사용하였습니다.
web::json::value를 이용해서 본격적으로 parsing 하는 것에 대해서는 내용이 많은 관계로 별도 Posting으로 작성하겠습니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cpprest/http_client.h> | |
#include <cpprest/json.h> | |
web::uri_builder GetBuilder(std::wstring p_sQueryPath, | |
std::vector<std::pair<std::wstring, std::wstring>>* p_pvQuery) | |
{ | |
web::uri_builder builder; | |
if (!p_sQueryPath.empty()) | |
{ | |
builder.set_path(p_sQueryPath); | |
if (!p_pvQuery->empty()) | |
{ | |
for (std::pair<std::wstring, std::wstring> pQuery : (*p_pvQuery)) | |
{ | |
builder.append_query(pQuery.first, pQuery.second); | |
} | |
} | |
} | |
return builder; | |
} | |
web::json::value GetJson(std::wstring p_sUrl, | |
std::wstring p_sQueryPath = U(""), | |
std::vector<std::pair<std::wstring, std::wstring>>* p_pvQuery = nullptr) | |
{ | |
web::json::value vJson; | |
web::http::client::http_client client(p_sUrl); | |
web::uri_builder builder = GetBuilder(p_sQueryPath, p_pvQuery); | |
pplx::task<void> requestTask = client.request(web::http::methods::GET, builder.to_string()) | |
.then([&](web::http::http_response response) { | |
return response.extract_json(); | |
}) | |
.then([&](pplx::task<web::json::value> previousTask) { | |
try | |
{ | |
vJson = previousTask.get(); | |
} | |
catch (const web::http::http_exception& e) | |
{ | |
printf("Error exception:%s\n", e.what()); | |
} | |
}); | |
try | |
{ | |
requestTask.wait(); | |
} | |
catch (const std::exception &e) | |
{ | |
printf("Error exception:%s\n", e.what()); | |
} | |
return vJson; | |
} | |
std::wstring GetString(std::wstring p_sUrl, | |
std::wstring p_sQueryPath = U(""), | |
std::vector<std::pair<std::wstring, std::wstring>>* p_pvQuery = nullptr) | |
{ | |
std::wstring sBody; | |
web::http::client::http_client client(p_sUrl); | |
web::uri_builder builder = GetBuilder(p_sQueryPath, p_pvQuery); | |
pplx::task<void> requestTask = client.request(web::http::methods::GET, builder.to_string()) | |
.then([&](web::http::http_response response) { | |
return response.extract_string(); | |
}) | |
.then([&](utility::string_t str) { | |
sBody = str; | |
}); | |
try | |
{ | |
requestTask.wait(); | |
} | |
catch (const std::exception &e) | |
{ | |
printf("Error exception:%s\n", e.what()); | |
} | |
return sBody; | |
} | |
int _tmain(int argc, _TCHAR* argv[]) | |
{ | |
std::wstring S = GetString(U("http://localhost:8080/RESTService/city")); | |
web::json::value J = GetJson(U("http://localhost:8080/RESTService/city")); | |
web::json::value J1 = web::json::value::parse(S); | |
return 0; | |
} |
댓글 없음:
댓글 쓰기