Installing the C + + REST SDK
$ brew install cpprestsdk$ brew install boost$ brew install libressl
Create a project
Open Xcode,file/new/project ...
On page 1th of the wizard, select Macos/command line Tool
On the 2nd page of the wizard, select C++,product name to fill in any name
on page 3rd of the wizard, select any folder and click Create to build the project.
Configuration Engineering
Set the System Header Search Paths to
/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
Set the Library Search Paths to
/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
Set the other Linker Flags to
-lcpprest-lboost_system-lboost_thread-mt-lboost_chrono-mt-lssl-lcrypto
cpprestsdk:undefined Symbols for Architecture x86_64
C + + code
#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) {*files Tream = 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 and handle any exceptions try {requesttask.wait (); } catch (const std::exception &e) {printf ("Error exception:%s\n", E.what ()); } return 0;}
Network programming using the C + + REST SDK