从CPPRest库(即casablanca)获得的结果中提取基本STL字符串

Basic STL string extraction from results obtained from CPPRest libraries (i.e. casablanca)?

本文关键字:提取 结果 字符串 STL CPPRest casablanca      更新时间:2023-10-16

我正在努力学习两件事:1) 一些基本的C++STL(我是一个老的C/C++程序员,试图学习新东西)2) 如何使用CPPRest库通过REST服务访问我的神童列表帐户。

我已经能够用Wunderlist成功地启动oauth2进程,但为了帮助我了解发生了什么以及返回了什么,我只想打印结果字符串。就我的一生而言,我不知道该怎么做。这与操作iostream有关,但由于我是这些方面的新手,所以我很挣扎。

这里有一个代码片段,它成功地将HTML从最初的Wunderlist响应中获取到streambuf中,但我无法将其获取到字符串中进行打印(或其他任何操作)。请注意,我并不关心异步执行此操作;因此,我只是通过强制同步!task.is_done()。此外,如果你想编译和运行,你需要为Wunderlist提供自己的client_id,或者使用不同的服务。

#include "stdafx.h"
#include <cpprest/http_client.h>
#include <cpprest/oauth2.h>
using namespace utility;                    // Common utilities like string conversions
using namespace web;                        // Common features like URIs.
using namespace web::http;                  // Common HTTP functionality
using namespace web::http::client;          // HTTP client features
using namespace concurrency::streams;       // Asynchronous streams
using namespace web::http::oauth2::details;
int main()
{
    http_client clientWL(U("https://www.wunderlist.com"));
    uri_builder builderWL(U("oauth/authorize"));
    builderWL.append_query(U("client_id"), U("[myclientid]"));
    builderWL.append_query(U("redirect_uri"), U("http://www.google.com"));
    builderWL.append_query(U("state"), U("Randomish"));
    auto task = clientWL.request(methods::GET, builderWL.to_string());
    while (!task.is_done());
    http_response resp1 = task.get();
    Concurrency::streams::basic_ostream<char> os = Concurrency::streams::container_stream<std::string>::open_ostream();
    Concurrency::streams::streambuf<char> sb = os.streambuf();
    Concurrency::task<size_t> task2 = resp1.body().read_to_end(sb);
    while (!task2.is_done());
    // HOW DO I GET THE RESULTING HTML STRING I CAN PLAINLY SEE IN sb 
    // (VIA A DEBUGGER) INTO THE FOLLOWING STRING OBJECT?
    std::string strResult;
    return 0;
}

有一种独立于平台的方法可以从http_response对象中提取字符串:

http_response resp1 = task.get();
auto response_string = resp1.extract_string().get();
std::cout << response_string << std::endl; // Will print it to stdout