CSDN首页> 软件研发 微软在CodePlex上开源C++ REST SDK 代号Casablanca

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。

主要功能特征:

  1. 通过HTTP客户端即可连接到服务器,发送请求和做出响应
  2. 支持统一资源标识符(URI)
  3. 构造、解析和序列化JSON值
  4. 通过流和流缓冲区从底层介质中读/写字节

针对上面这些特征,提供一些代码实例。

示例一:更新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.
     uri serverUri(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)
       {
       }
     });
   });
}
示例二:在内存中创建JSON值并且遍历这些值

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());
   }
}

更多信息:

  1. Casablanca on DevLabs
  2. Casablanca论坛
  3. 源码地址

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值