微軟開(kāi)源 C++ REST SDK
微軟的代號(hào)為Casablanca的C++ REST SDK已經(jīng)基于Apache許可證開(kāi)源。它被描述為“微軟為了以原生代碼支持基于云的客戶端/服務(wù)器通信所做的努力,采用了現(xiàn)代異步C++ API設(shè)計(jì)”。該產(chǎn)品使用C++11實(shí)現(xiàn),微軟希望提供一種更簡(jiǎn)單的編寫客戶端HTTP代碼的方法。
Casablanca支持多個(gè)平臺(tái),除了Windows 7、Windows 8之外還支持Linux。微軟的開(kāi)發(fā)人員Artur Laksberg提到,對(duì)WinXP和Vista的支持正在開(kāi)發(fā)之中。該產(chǎn)品的另一個(gè)亮點(diǎn)是支持異步操作。微軟在公布時(shí)提供了一些例子來(lái)說(shuō)明Casablanca的使用,一個(gè)是通過(guò)HTTP上傳文件,一個(gè)是JSON對(duì)象的創(chuàng)建。
Windows和Linux上的構(gòu)建版本都支持以下特性:
- 能夠通過(guò)HTTP客戶端創(chuàng)建到服務(wù)器的連接,并能發(fā)送請(qǐng)求和處理響應(yīng)。
- 支持URI的構(gòu)建與使用。
- 能夠構(gòu)建、解析和序列化JSON值。
- 可以通過(guò)流(Stream)和流緩沖(Stream Buffer)對(duì)底層介質(zhì)進(jìn)行異步的數(shù)據(jù)讀寫。
Casablanca中有幾種不同的流和流緩沖可供使用:基于內(nèi)存的生產(chǎn)者/消費(fèi)者、文件、可以配合STL容器使用的基于內(nèi)存的流、裸指針流和互操作流。互操作流使得“Casablanca能夠提供兩組類,一組使用異步流到iostream的接口,另一組使用iostream到異步流的接口”。
Linux HTTP客戶端還有些限制,因?yàn)樗胁恢С諬TTPS、代理和認(rèn)證,但微軟介紹說(shuō)這些特性會(huì)包含在未來(lái)的版本中。Casablanca的源代碼放在了CodePlex上,可以在線查看或通過(guò)Git獲取,還可以以Zip包形式下載最新的快照版本。
C++ REST SDK 包含在 Casablanca 項(xiàng)目中。Casablanca 是一個(gè) C++ 本地庫(kù),旨在幫助開(kāi)發(fā)者的 C++ 應(yīng)用程序訪問(wèn)云服務(wù)。如果你想編寫一個(gè)響應(yīng)式的 C++ 客戶端應(yīng)用程序,或者是一個(gè)可擴(kuò)展的服務(wù)端解決方案,可以試試 Casablanca。除了C++ REST SDK 外,Casablanca 項(xiàng)目還包含 Azure SDK for C++。
C++ REST SDK 中包含了一些工具,可以幫助開(kāi)發(fā)者快速編寫現(xiàn)代、異步、可連接 REST 服務(wù)的 C++ 應(yīng)用程序,遵循C++11 標(biāo)準(zhǔn),目前支持 Windows 7、Windows 8(包括 Windows Store 和桌面應(yīng)用)和 Linux。
該 SDK 的主要特性包括:
- 能夠通過(guò) HTTP Client 創(chuàng)建服務(wù)器連接,并發(fā)送請(qǐng)求、處理響應(yīng)
- 支持構(gòu)造和使用 URI(Uniform Resource Identifiers,統(tǒng)一資源標(biāo)識(shí)符)
- 構(gòu)造、解析和序列化 JSON 值
- 通過(guò) Streams 和 Stream Buffers 從底層介質(zhì)異步讀取/寫入字節(jié)
下面的示例演示了如何上傳文件到 HTTP 服務(wù)器:
#include <http_client.h>
#include<filestream.h>
#include <uri.h> using namespace concurrency::streams;
using namespace web::http::client;
using namespace web::http;
int main ()
{
// Open stream to file. file_stream<unsigned char>::open_istream (L"myfile.txt") .then ([](basic_istream<unsigned char> fileStream)
{
// Make HTTP request with the file stream as the body. http_client client (L"http://www.myhttpserver.com");
client.request (methods::PUT, L"myfile", fileStream) .then ([fileStream](http_response response)
{
fileStream.close ();
// Perform actions here to inspect the HTTP response... if(response.status_code () == status_codes::OK)
{
}
});
});
return 0;
}
下面的示例演示了如何構(gòu)建并遍歷 JSON 值:
#include <json.h>
int main ()
{
// Create a JSON object. json::value obj;
obj[L"key1"] = json::value::boolean (false);
obj[L"key2"] = json::value::number (44);
obj[L"key3"] = json::value::number (43.6);
obj[L"key4"] = json::value::string(U("str"));
// Loop over each element in the object. for(auto iter = obj.cbegin (); iter != obj.cend (); ++iter)
{
// Make sure to get the value as const reference otherwise you will end up copying
// the whole JSON value recursively which can be expensive if it is a nested object. const json::value &str = iter->first;
const json::value &v = iter->second;
// Perform actions here to process each string and value in the JSON object... wprintf (L"String:%s", str.as_string ());
wprintf (L"Value:%s", v.to_string ());
}
return 0;
}
詳細(xì)信息:The C++ REST SDK ("Casablanca")
Using the Microsoft C++ REST SDK
歡迎大家掃描下面二維碼成為我的客戶,扶你上云

浙公網(wǎng)安備 33010602011771號(hào)