【REST SDK】初探C++ REST SDK(Casablanca)

來源:互聯網
上載者:User

一直苦惱在C++下沒一個好用的http庫,福士一點的就是curl了,以前也試用過它,阻塞的設計,每個線程只可以有一個控制代碼。。總之我覺得不好用

今天偶然發現了微軟的一個開源項目Casablanca,試用了一下感覺棒極了,它使用了現在最前沿的C++11標準,寫起代碼來就是一個字:爽!


為了使用這麼前衛的庫,建議VS在2012以上。2010版以前貌似是用不了的。

先看看自己裝了"NuGet封裝管理員"沒有,沒有則先裝上它


然後進入安裝包管理主控台




輸入安裝命令"install-package cpprestsdk"




稍等片刻後,最新版的rest sdk就安裝好了,安裝目錄在"C:\Program Files (x86)\Microsoft SDKs\Cpp REST SDK for Visual Studio 2013"

後面就是建立工程,引入標頭檔和lib檔案即可


#include "stdafx.h"#include <iostream>#include <http_client.h>#pragma comment(lib, "casablanca120.lib")using namespace std;using namespace web::http;using namespace web::http::client;int _tmain(int argc, _TCHAR* argv[]){http_client cli(U("http://www.baidu.com"));// get方式請求服務auto response = cli.request(methods::GET, U("s?wd=123"));// 指定一個回呼函數,也就是匿名函數(lambda)auto task = response.then([](http_response hr){cout << "error_code: " << hr.error_code() << endl;cout << "status code: " << hr.status_code() << endl;});// 阻塞,等待請求完成task.wait();system("pause");return 0;}

執行結果




ps:C++11的auto真是個好東西有木有!?再也不怕那些名字又臭又長的類型了!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.