- 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)
Web Page의 결과 ( html, xml, json 등... ) 을 std::wstring 으로 가져오는 소스 코드 입니다.
https://msdn.microsoft.com/ko-kr/library/jj950081.aspx
위 MSDN 에제를 참조하여 작성하였습니다.
Break Point를 찍어서 s, s1 에 저장된 문자열을 별도의 파일에다가 저장 한 후 웹 브라우저 에서 열면 결과 확인이 가능합니다.
MSDN에 있는 예제는 바로 File 로 저장을 하는 Code인데, json Parsing을 다음에 다룰 예정이라서 일단은 범용으로 사용 하도록 std::wstring 으로 저장하도록 구현하였습니다.
Project에 C++ REST SDK를 적용하는 방법은 다음 Post를 참고하시면 됩니다.
http://devluna.blogspot.kr/2015/05/c-resk-sdk-in-visual-studio-2013.html
소스 코드는 다음과 같습니다.
This file contains hidden or 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> | |
std::wstring GetWebString(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; | |
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); | |
} | |
} | |
} | |
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 = GetWebString(U("http://devluna.blogspot.kr")); | |
std::vector<std::pair<std::wstring, std::wstring>> v{ { U("q"), U("DevStory+of+LunaStar") } }; | |
std::wstring s2 = GetWebString(U("http://www.bing.com/"), U("search"), &v); | |
return 0; | |
} | |
댓글 없음:
댓글 쓰기