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"

最新文章

  1. Linux 命令执行结果输出到屏幕的同时写入到文件中
  2. Delphi中record和packed record的区别
  3. 使用yuicompressor-maven-plugin压缩js及css文件
  4. FIFO、LRU、OPT这三种置换算法的缺页次数
  5. [liu yanling]测试方法
  6. Linux 下如何使用看门狗
  7. highlight高亮
  8. 百度java开发面试题
  9. Java对象模型规约
  10. Oracle DataGuard 11g 双机实验
  11. 【读书笔记】iOS-Apple的移动设备硬件
  12. sorted 返回字典的所有键
  13. oracle move 释放 表空间
  14. js之 data-*自定义属性
  15. Codeforces Beta Round #44 (Div. 2)
  16. 使用express框架和mongoose在MongoDB更新数据
  17. iOS 使用代码创建约束,实现自动布局
  18. [Python学习笔记-005] 理解yield
  19. 【Android自动化】unittest测试框架关于用例执行的几种方法
  20. 20155306 白皎 0day漏洞——漏洞的复现

热门文章

  1. TC SRM600 DIV2
  2. VIM简单配置(windows)
  3. 在windows server 2008 R2 64bit上面配置PI OPC Server的DCOM
  4. 【Go命令教程】2. go build
  5. Nginx FastCGI的运行原理
  6. oracle HA 高可用性具体解释(之二,深入解析TAF,以及HA框架)
  7. 在ASP.NET MVC4中实现同页面增删改查,无弹出框01,Repository的搭建
  8. 低版本系统兼容的ActionBar(四)添加Tab+添加自定义的Tab视图+Fragment
  9. 低版本系统兼容的ActionBar(一)设置颜色+添加Menu+添加ActionMode
  10. HTML5 本地文件操作之FileSystemAPI实例(三)