菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
348
0

C++ REST SDK i

原创
05/13 14:22
阅读数 8773

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

Vcpkg package
Homebrew package
Ubuntu 18.04 package
Fedora Rawhide package
openSUSE Tumbleweed package
Debian Testing package

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文件所在的代码替换成下面例子的代码:

 

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



 

 

 

编译,运行,结果:

 

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

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

 

发表评论

0/200
348 点赞
0 评论
收藏