Welcome!

The C++ REST SDK is a Microsoft project for cloud-based client-server communication in native code using a modern asynchronous C++ API design. This project aims to help C++ developers connect to and interact with services.

Getting Started






With vcpkg on Windows

PS> vcpkg install cpprestsdk cpprestsdk:x64-windows

With apt-get on Debian/Ubuntu

$ sudo apt-get install libcpprest-dev

With brew on OSX

$ brew install cpprestsdk

With NuGet on Windows for Android

PM> Install-Package cpprestsdk.android

For other platforms, install options, how to build from source, and more, take a look at our Documentation.

Once you have the library, look at our tutorial to use the http_client. It walks through how to setup a project to use the C++ Rest SDK and make a basic Http request.

To use from CMake:

cmake_minimum_required(VERSION 3.7)
project(main) find_package(cpprestsdk REQUIRED) add_executable(main main.cpp)
target_link_libraries(main PRIVATE cpprestsdk::cpprest)

What's in the SDK:

  • Features - HTTP client/server, JSON, URI, asynchronous streams, WebSockets client, oAuth
  • PPL Tasks - A powerful model for composing asynchronous operations based on C++ 11 features
  • Platforms - Windows desktop, Windows Store (UWP), Linux, OS X, Unix, iOS, and Android
  • Support for Visual Studio 2015 and 2017 with debugger visualizers

Contribute Back!

Is there a feature missing that you'd like to see, or found a bug that you have a fix for? Or do you have an idea or just interest in helping out in building the library? Let us know and we'd love to work with you. For a good starting point on where we are headed and feature ideas, take a look at our requested features and bugs.

Big or small we'd like to take your contributions back to help improve the C++ Rest SDK for everyone. If interested contact us askcasablanca at Microsoft dot com.

Having Trouble?

We'd love to get your review score, whether good or bad, but even more than that, we want to fix your problem. If you submit your issue as a Review, we won't be able to respond to your problem and ask any follow-up questions that may be necessary. The most efficient way to do that is to open a an issue in our issue tracker.

Quick Links

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

安装微软的开源 cpprestsdk  (C++ REST SDK (codename "Casablanca")),要先有项目;这里新建一个WIN32控制台项目,名为XXX,默认使用系统生成的代码;

然后打开:VS2013 -> 工具 ->库程序包管理器->程序包管理器控制台

输入 :

install-package cpprestsdk

等待安装完毕;

或者慢的话,到 https://www.nuget.org/packages?q=cpprestsdk.v120

手动把这几个包下载下来(点击进去,点download)放到缓存目录: C:\Users\Administrator\AppData\Local\NuGet\Cache

再执行 install-package cpprestsdk

等待安装

显示

。。。

已成功将“cpprestsdk 2.9.1.1”添加到 xxx (你新建的项目名),则安装成功。

把main文件所在的代码替换成下面例子的代码:

  1. // xx.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. /*
  5. int _tmain(int argc, _TCHAR* argv[])
  6. {
  7. return 0;
  8. }
  9. */
  10. #include <cpprest/http_client.h>
  11. #include <cpprest/json.h>
  12. //#include <http_client.h>
  13. #include <iostream>
  14. //#include <json.h>
  15. using namespace web;
  16. using namespace web::http;
  17. using namespace web::http::client;
  18. using namespace std;
  19. // Retrieves a JSON value from an HTTP request.
  20. pplx::task<void> RequestJSONValueAsync()
  21. {
  22. // TODO: To successfully use this example, you must perform the request
  23. // against a server that provides JSON data.
  24. // This example fails because the returned Content-Type is text/html and not application/json.
  25. //http_client client(L"http://www.fourthcoffee.com");
  26. http_client client(L"http://www.fourthcoffee.com");
  27. return client.request(methods::GET).then([](http_response response) -> pplx::task<json::value>
  28. {
  29. if (response.status_code() == status_codes::OK)
  30. {
  31. wcout<< response.extract_string().get().c_str()<<endl;
  32. return response.extract_json();
  33. }
  34. // Handle error cases, for now return empty json value...
  35. return pplx::task_from_result(json::value());
  36. })
  37. .then([](pplx::task<json::value> previousTask)
  38. {
  39. try
  40. {
  41. const json::value& v = previousTask.get();
  42. // Perform actions here to process the JSON value...
  43. }
  44. catch (const http_exception& e)
  45. {
  46. // Print error.
  47. wostringstream ss;
  48. ss << e.what() << endl;
  49. wcout << ss.str();
  50. }
  51. });
  52. /* Output:
  53. Content-Type must be application/json to extract (is: text/html)
  54. */
  55. }
  56. // Demonstrates how to iterate over a JSON object.
  57. void IterateJSONValue()
  58. {
  59. // Create a JSON object.
  60. json::value obj;
  61. obj[L"key1"] = json::value::boolean(false);
  62. obj[L"key2"] = json::value::number(44);
  63. obj[L"key3"] = json::value::number(43.6);
  64. obj[L"key4"] = json::value::string(U("str"));
  65. // Loop over each element in the object.
  66. for (auto iter = obj.as_object().cbegin(); iter != obj.as_object().cend(); ++iter)
  67. {
  68. // Make sure to get the value as const reference otherwise you will end up copying
  69. // the whole JSON value recursively which can be expensive if it is a nested object.
  70. //const json::value &str = iter->first;
  71. //const json::value &v = iter->second;
  72. const auto &str = iter->first;
  73. const auto &v = iter->second;
  74. // Perform actions here to process each string and value in the JSON object...
  75. std::wcout << L"String: " << str.c_str() << L", Value: " << v.serialize() << endl;
  76. }
  77. /* Output:
  78. String: key1, Value: false
  79. String: key2, Value: 44
  80. String: key3, Value: 43.6
  81. String: key4, Value: str
  82. */
  83. }
  84. int wmain()
  85. {
  86. // This example uses the task::wait method to ensure that async operations complete before the app exits.
  87. // In most apps, you typically don�t wait for async operations to complete.
  88. wcout << L"Calling RequestJSONValueAsync..." << endl;
  89. RequestJSONValueAsync().wait();
  90. wcout << L"Calling IterateJSONValue..." << endl;
  91. IterateJSONValue();
  92. getchar();
  93. }

编译,运行,结果:

.............

d)/*]]>*/</script></body></html>
Calling IterateJSONValue...
String: key1, Value: false
String: key2, Value: 44
String: key3, Value: 43.600000000000001
String: key4, Value: "str"

C++ REST SDK i的更多相关文章

  1. 配置android sdk 环境

    1:下载adnroid sdk安装包 官方下载地址无法打开,没有vpn,使用下面这个地址下载,地址:http://www.android-studio.org/

  2. 阿里云直播 C# SDK 如何使用

    阿里云直播SDK的坑 1.直播云没有单独的SDK,直播部分被封装在CDN的相关SDK当中. 2.针对SDK,没有相关Demo. 3.针对SDK,没有相关的文档说明. 4.针对SDK的说明,官网上的说明 ...

  3. 使用Visual Studio SDK制作GLSL词法着色插件

    使用Visual Studio SDK制作GLSL词法着色插件 我们在Visual Studio上开发OpenGL ES项目时,避免不了写Shader.这时在vs里直接编辑shader就会显得很方便. ...

  4. iOS开发之App间账号共享与SDK封装

    上篇博客<iOS逆向工程之KeyChain与Snoop-it>中已经提到了,App间的数据共享可以使用KeyChian来实现.本篇博客就实战一下呢.开门见山,本篇博客会封装一个登录用的SD ...

  5. Intel Media SDK H264 encoder GOP setting

    1 I帧,P帧,B帧,IDR帧,NAL单元 I frame:帧内编码帧,又称intra picture,I 帧通常是每个 GOP(MPEG 所使用的一种视频压缩技术)的第一个帧,经过适度地压缩,做为随 ...

  6. Android SDK 在线更新镜像服务器资源

    本文转自:http://blog.kuoruan.com/24.html.感谢原作者. 什么是Android SDK SDK:(software development kit)软件开发工具包.被软件 ...

  7. TYPESDK手游聚合SDK服务端设计思路与架构之二:服务端设计

    在前一篇文中,我们对一个聚合SDK服务端所需要实现的功能作了简单的分析.通过两个主要场景的功能流程图,我们可以看到,作为多款游戏要适配多个渠道的统一请求转发中心,TYPESDK服务端主要需要实现的功能 ...

  8. TYPESDK手游聚合SDK服务端设计思路与架构之一:应用场景分析

    TYPESDK 服务端设计思路与架构之一:应用场景分析 作为一个渠道SDK统一接入框架,TYPESDK从一开始,所面对的需求场景就是多款游戏,通过一个统一的SDK服务端,能够同时接入几十个甚至几百个各 ...

  9. Android SDK 与API版本对应关系

    Android SDK版本号 与 API Level 对应关系如下表: Code name Version API level   (no code name) 1.0 API level 1   ( ...

  10. Kotlin与Android SDK 集成(KAD 05)

    作者:Antonio Leiva 时间:Dec 19, 2016 原文链接:https://antonioleiva.com/kotlin-integrations-android-sdk/ 使用Ko ...

随机推荐

  1. socket listen参数中的backlog

    服务器监听时,在每次处理一个客户端的连接时是需要一定时间的,这个时间非常的短(也许只有1ms 或者还不到),但这个时间还是存在的.而这个backlog 存在的意义就是:在这段时间里面除了第一个连接请求 ...

  2. 移植Python3到TQ2440(一)

    平台 硬件:TQ2440  64MB内存 256MB NandFlash bootloader:U-Boot 2015.04 kernel:linux-4.9 Python: Python-3.6.0 ...

  3. Java Calendar,Date,DateFormat,TimeZone,Locale等时间相关内容的认知和使用(5) SimpleDateFormat

    本章介绍SimpleDateFormat. SimpleDateFormat 介绍 SimpleDateFormat 是一个格式化Date 以及 解析日期字符串 的工具.它的最常用途是,能够按照指定的 ...

  4. 哥谭第一季/全集Gotham迅雷下载

    哥谭市 第一季 Gotham (2014)本季看点:<哥谭镇>将由丹尼·加农掌镜,布鲁诺·海勒执笔剧本.电视剧集<哥谭>可以说<蝙蝠侠>的前传,剧集描述的是DC漫画 ...

  5. 快速找到自己想要用到的cocos2d-x的缓冲动画

    游戏中在做很多动画时,需要用到缓冲来增强表现.比如宝箱"鼓"几下,然后"蹦"的一下打开.很多时候要调效果时,需要轮着试,如果有一张图和实际示例效果,那就省很多事 ...

  6. 详细解读Volley(五)—— 通过源码来分析业务流程

    一.初始化请求队列并运行 我们用Volley时,最先开始的就是初始化请求队列,一种常见的写法如下: public class MyApplication extends Application { p ...

  7. [转]PHP中替换换行符

    FROM :http://www.cnblogs.com/siqi/archive/2012/10/12/2720713.html //php 有三种方法来解决 //1.使用str_replace 来 ...

  8. kafka-python的gevent模式和kafka的兼容性

    使用gevent会杀死kafka的consumer线程:据查:kafka-python对gevent的支持不是太好,可以使用pykafka:但是可以kafka-python可以结合eventlet使用 ...

  9. 数学图形之贝塞尔(Bézier)曲面

    前面章节中讲了贝塞尔(Bézier)曲线,而贝塞尔曲面是对其多一个维度的扩展.其公式依然是曲线的公式: . 而之所以由曲线变成曲面,是将顶点横向连了再纵向连. 很多计算机图形学的教程都会有贝塞尔曲面的 ...

  10. Shell bc命令进行数学运算

    通常情况做简单的运算,很多命令里面都是支持的.比如for, awk等. #!/bin/bash num= #for循环这里的数字也是运算 #也可以使用 #也可以使用数组 ;i<=;++i)) d ...