Casablancaã®ã¤ã³ã¹ãã¼ã«
çããããã«ã¡ã¯
ãå
æ°ã§ãããç§ã¯çãå°½ãã¾ããã
ä»æ¥ã¯Casablancaã®ã¤ã³ã¹ãã¼ã«è¨äºãè¨è¼ãã¾ãã
Casablancaã¨ã¯
ãã¤ã¯ãã½ããã®C++ REST SDKã§ãã¯ã©ã¦ããã¼ã¹ã®ã¯ã©ã¤ã¢ã³ãã»ãµã¼ãã¼ã®
APIãã¶ã¤ã³ã ãããªã
C++ REST SDK (codename "Casablanca") - Home
å
容ã«ã¯HTTP Client/Server, JSON, URL ,Asynchronus Stream,WebSockets,OAuthãæè¼ããã¦ãããããªã
å ã¿ã«ãã¤ã¯ãã½ããã ããã¨ãã£ã¦ãWindowsã ãã§ãªãLinuxãMacã§ã使ãã¾ãã
å ã¿ã«åã¯MacOSã§ãã
Install
å¿ è¦ãªãã®
brew install cmake git openssl boost
å¿ è¦ãªãã®ãã¤ã³ã¹ãã¼ã«ããã¦ããã®ã確èªãã¦ãã
git clone https://git01.codeplex.com/casablanca cd casablanca mkdir build.release cd build.release cmake ../Release -DCMAKE_BUILD_TYPE=Release make -j 4
以ä¸ã®ãµã³ãã«ã³ã¼ããåããã«ã¯ã©ãããã°ãããèãã¦ã¿ã¾ãããã
ã¶ã£ã¡ããmake installã¨å¼ã°ããç´ æ´ãããã³ãã³ãã¯ãªãã®ã§èªåã§ã
#include <cpprest/http_client.h> #include <cpprest/filestream.h> using namespace utility; // Common utilities like string conversions using namespace web; // Common features like URIs. using namespace web::http; // Common HTTP functionality using namespace web::http::client; // HTTP client features using namespace concurrency::streams; // Asynchronous streams int 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("Casablanca CodePlex")); 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; }
åé¡ç¹ãï¼ã¤ããã¾ãã
â cpprestã®headerã使ããã¨
ãããããã¯é©å½ã«æ¾ãè¾¼ã¿ã¾ãããå
¥ããå ´æã®åèã«ãã¦ãã ããã
è¦ã¯includeã§ããã°ãªãã§ãããã®ã§ããã»ã»ã»ã»
cp -r $(PATH)/casablanca/Release/include/ /usr/local/include/
â¡ãªã³ã¯ãã³ã³ãã¤ã«ã§ãã
以ä¸ã®$(PATH)ã¯casablancaã«è³ãã¾ã§ã®ãã¹ã§ãããã®ä¸ã«ãã.dylibãå
¨ã¦ãªã³ã¯ããã¾ãã
ã¾ã -lã使ããä¸ã§å©ç¨ããã¦ããboost_systemãåä½ããã¾ãã
ææããªãã¸ã§ã¯ãã足ãã¾ããã¨ã©ã¼ãåºãã®ã§è¾æ±å¼·ãé å¼µãã¾ããã
g++ test.o $(PATH)/casablanca/build.release/Binaries/*.dylib -o main -I /usr/local/include/ /usr/local/lib/libboost_thread-mt.a -lboost_system /usr/local/lib/libboost_chrono-mt.a
ã³ã³ãã¤ã«ãã¦ãã¾ãããã¨ä»¥ä¸ã®æ§ãªã¡ãã»ã¼ã¸ãåºã¦ãã¾ãã
Received response status code:200
ç´°ãã使ãæ¹ã¯æ¬¡å以éã«åãã¾ãã
ä½ãããè¨å®ã¯ãªãã§ããã»ã»ã»ã»ã»ã»
make installã®å®è£
ãå¸æãããã¨ããã»ã»ã»ã»