CPP REST SDK是微軟開源的基于PPL的異步http client,網絡層使用的是Boost.Asio,跨平臺,并且支持json解析,在使用CPP REST SDK之前要確保已經安裝了boost和openssl
安裝boost和openssl
yum install boost-devel -y
yum install openssl-devel -y安裝cpprestsdk
git clone https:斜杠github.com/microsoft/cpprestsdk
cd cpprestsdk
git submodule update --init
mkdir build && cd build
cmake ..
make -j4
make install報錯:
cpprestsdk/Release/tests/functional/http/listener/listener_construction_tests.cpp:529:17: error: ‘class boost::asio::ssl::context’ has no member named ‘use_certificate_chain’; did you mean ‘use_certificate_file’?原因:yum安裝boost只有1.53版本,要手動安裝1.63.0版本
解決:手動安裝boost
源碼下載:https:斜杠boostorg.jfrog.io/artifactory/main/release/
cd boost_1_63_0
./bootstrap.sh
./b2
./b2 install使用示例
從 HTTP GET 響應提取 JSON 數據
// Retrieves a JSON value from an HTTP request.
pplx::task<void> RequestJSONValueAsync()
{
    // TODO: To successfully use this example, you must perform the request  
    // against a server that provides JSON data.  
    // This example fails because the returned Content-Type is text/html and not application/json.
    http_client client(L"https:斜杠abc.def.com");
    return client.request(methods::GET).then([](http_response response) -> pplx::task<json::value>
    {
        if(response.status_code() == status_codes::OK)
        {
            return response.extract_json();
        }
        // Handle error cases, for now return empty json value... 
        return pplx::task_from_result(json::value());
    })
        .then([](pplx::task<json::value> previousTask)
    {
        try
        {
            const json::value& v = previousTask.get();
            // Perform actions here to process the JSON value...
        }
        catch (const http_exception& e)
        {
            // Print error.
            wostringstream ss;
            ss << e.what() << endl;
            wcout << ss.str();
        }
    });
    /* Output:
    Content-Type must be application/json to extract (is: text/html)
    */
}