http://www.csdn.net/article/2013-02-27/2814279-Casablanca-C++-REST-SDK
摘要:微软开源Casablanca C++ REST SDK,代码托管在CodePlex上,支持Windows 7、Windows8(Windows store和桌面应用程序)、Linux系统。
Casablanca是C++开发人员的一组类库,旨在让C++程序员使用和执行RESTful服务变得更简单。该项目主要包含两个SDK,一个是本次开源的C++ REST SDK,另外一个是C++ Azure SDK。
目前C++ REST SDK的源码托管在CodePlex上。使用C++ REST SDK工具包可以快速开发出现代、异步的C++代码并且连接到REST服务上,此外,还在C++ 11里提供一个跨平台解决方案。目前支持Windows 7、Windows8(Windows store和桌面应用程序)、Linux。
主要功能特征:- 通过HTTP客户端即可连接到服务器,发送请求和做出响应
- 支持统一资源标识符(URI)
- 构造、解析和序列化JSON值
- 通过流和流缓冲区从底层介质中读/写字节
针对上面这些特征,提供一些代码实例。
示例一:更新HTTP服务端文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
#include <http_client.h>
#include<filestream.h>
#include <uri.h>
using
namespace
concurrency::streams;
using
namespace
web::http::client;
using
namespace
web::http;
void
main()
{
// Open stream to file.
file_stream<unsignedchar>::open_istream(L
"myfile.txt"
).then([](basic_istream<unsignedchar> fileStream)
{
// Make HTTP request with the file stream as the body.
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)
{
}
});
});
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
#include <json.h>
void
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());
}
}
|
更多信息: