반응형
C++ REST SDK를 통해서 파일로 받는 방법은 아래 링크를 참조하시면 됩니다.
2015/06/25 - [Programming/C&C++] - C++ REST SDK(카사블랑카)로 웹페이지 가져오기
일반적으로 HTTP클라이언트는 파일을 생성하지 않습니다.
받은 결과를 직접 파싱해서 사용하는 것이 일반적인 방식입니다.
std::wstring으로 결과를 전달받는 전체 코드는 다음과 같습니다.
wstring으로 전달 받은 이후에 XML 파서(Parser) 등으로 원하는 값을 출력하면 됩니다.
#include <string> // std::wstring
#include <vector> // std::vector
#include <utility> // std::pair
#include <cpprest/http_client.h>
int main()
{
// return wstring value
std::wstring szBody;
// vector of Query
std::vector<std::pair<std::wstring, std::wstring>> vQuery{ { U("q"), U("Casablanca CodePlex") } };
// make uri
web::http::client::http_client client(U("http://www.bing.com/"));
web::uri_builder builder(U("/search"));
for (auto query : vQuery)
{
builder.append_query(query.first, query.second);
}
// make task(request and get result)
auto 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)
{
szBody = str;
});
try
{
requestTask.wait();
}
catch (const std::exception &e)
{
printf("Error exception:%s\n", e.what());
}
return 0;
}
파일을 열지 않아도 되기 때문에 간결하게 작성되었습니다.
Query를 생성하고 task를 생성해서 요청한 이후에 결과를 wstring으로 전달받습니다.
그리고 main() 함수 내의 szBody에 전달하고 완료됩니다.
이후에는 XML 파서로 처리하면 됩니다.
반응형
'Programming > C&CPP' 카테고리의 다른 글
| Visual C++ 확장 문법 property (0) | 2015.11.04 |
|---|---|
| shared_ptr에서 배열(array) delete[]로 해제하는 방법 (0) | 2015.08.27 |
| C++ REST SDK(카사블랑카)로 웹페이지 가져오기 (0) | 2015.06.25 |
| C++ REST SDK(Casablanca) 설치 방법 (0) | 2015.06.12 |
| wcout, wprintf 등에서 한글 출력 안될 때 해결법 (0) | 2015.01.19 |