標籤:pow nal modern cbe method cmake ber serialize mos
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
- FAQ
- Documentation
- Issue Tracker
- Directly contact us: [email protected]
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] 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
- // xx.cpp : 定義控制台應用程式的進入點。
- //
-
- #include "stdafx.h"
-
- /*
- int _tmain(int argc, _TCHAR* argv[])
- {
- return 0;
- }
- */
-
- #include <cpprest/http_client.h>
- #include <cpprest/json.h>
-
- //#include <http_client.h>
- #include <iostream>
- //#include <json.h>
-
- using namespace web;
- using namespace web::http;
- using namespace web::http::client;
-
- using namespace std;
-
- // Retrieves a JSON value from an HTTP request.
- pplx::task<void> RequestJSONValueAsync()
- {
- // TODO: To successfully use this example, you must perform the request
- // against a server that provides JSON data.
- // This example fails because the returned Content-Type is text/html and not application/json.
- //http_client client(L"http://www.fourthcoffee.com");
- http_client client(L"http://www.fourthcoffee.com");
- return client.request(methods::GET).then([](http_response response) -> pplx::task<json::value>
- {
- if (response.status_code() == status_codes::OK)
- {
- wcout<< response.extract_string().get().c_str()<<endl;
- return response.extract_json();
- }
-
- // Handle error cases, for now return empty json value...
- return pplx::task_from_result(json::value());
- })
- .then([](pplx::task<json::value> previousTask)
- {
- try
- {
- const json::value& v = previousTask.get();
- // Perform actions here to process the JSON value...
- }
- catch (const http_exception& e)
- {
- // Print error.
- wostringstream ss;
- ss << e.what() << endl;
- wcout << ss.str();
- }
- });
-
- /* Output:
- Content-Type must be application/json to extract (is: text/html)
- */
- }
-
- // Demonstrates how to iterate over a JSON object.
- void IterateJSONValue()
- {
- // 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.as_object().cbegin(); iter != obj.as_object().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;
-
- const auto &str = iter->first;
- const auto &v = iter->second;
-
- // Perform actions here to process each string and value in the JSON object...
- std::wcout << L"String: " << str.c_str() << L", Value: " << v.serialize() << endl;
- }
-
- /* Output:
- String: key1, Value: false
- String: key2, Value: 44
- String: key3, Value: 43.6
- String: key4, Value: str
- */
- }
-
- int wmain()
- {
- // This example uses the task::wait method to ensure that async operations complete before the app exits.
- // In most apps, you typically don?t wait for async operations to complete.
-
- wcout << L"Calling RequestJSONValueAsync..." << endl;
- RequestJSONValueAsync().wait();
-
- wcout << L"Calling IterateJSONValue..." << endl;
- IterateJSONValue();
-
- getchar();
- }
編譯,運行,結果:
.............
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