使用 C++ REST SDK 進行網路編程

來源:互聯網
上載者:User

標籤:creat   file   catch   status   into   exception   search   streambuf   ali   

安裝 C++ REST SDK
$ brew install cpprestsdk$ brew install boost$ brew install libressl
建立工程

開啟 Xcode,File / New / Project...
在嚮導的第1頁選 macOS / Command Line Tool
在嚮導的第2頁語言選 C++,Product Name 填上任意名稱
在嚮導的第3頁選擇任意檔案夾,點擊 Create 建立工程。

配置工程

將 System Header Search Paths 設定為
/usr/local/Cellar/cpprestsdk/2.10.2/include
/usr/local/Cellar/boost/1.67.0_1/include
/usr/local/Cellar/libressl/2.7.4/include

將 Library Search Paths 設定為
/usr/local/Cellar/cpprestsdk/2.10.2/lib
/usr/local/Cellar/boost/1.67.0_1/lib
/usr/local/Cellar/libressl/2.7.4/lib

將 Other Linker Flags 設定為
-lcpprest -lboost_system -lboost_thread-mt -lboost_chrono-mt -lssl -lcrypto

cpprestsdk: Undefined symbols for architecture x86_64

C++ 代碼
#include <cpprest/http_client.h>#include <cpprest/filestream.h>using namespace utility;                    // Common utilities like string conversionsusing namespace web;                        // Common features like URIs.using namespace web::http;                  // Common HTTP functionalityusing namespace web::http::client;          // HTTP client featuresusing namespace concurrency::streams;       // Asynchronous streamsint main(int argc, char* argv[]){    auto fileStream = std::make_shared<ostream>();    // Open stream to output file.    pplx::task<void> requestTask = fstream::open_ostream(U("results.html")).then([=](ostream outFile)    {        *fileStream = outFile;        // Create http_client to send the request.        http_client client(U("http://www.bing.com/"));        // Build request URI and start the request.        uri_builder builder(U("/search"));        builder.append_query(U("q"), U("cpprestsdk github"));        return client.request(methods::GET, builder.to_string());    })    // Handle response headers arriving.    .then([=](http_response response)    {        printf("Received response status code:%u\n", response.status_code());        // Write response body into the file.        return response.body().read_to_end(fileStream->streambuf());    })    // Close the file stream.    .then([=](size_t)    {        return fileStream->close();    });    // Wait for all the outstanding I/O to complete and handle any exceptions    try    {        requestTask.wait();    }    catch (const std::exception &e)    {        printf("Error exception:%s\n", e.what());    }    return 0;}

使用 C++ REST SDK 進行網路編程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.