从CPPRest库(即casablanca)获得的结果中提取基本STL字符串
Basic STL string extraction from results obtained from CPPRest libraries (i.e. casablanca)?
我正在努力学习两件事: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
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- valgrind-hellgrind与泄漏检查的结果不同
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 用C++20 fmt限制结果的总大小
- 如何返回一个类的两个对象相加的结果
- 如何从 std::atomic 中提取指针 T<T>?
- 为什么istream不支持右值提取
- 使用QProcess执行命令,并将结果存储在QStringList中
- 如果我std::dynamic_pointer_cast并且底层dynamic_cast的结果为null,那么返回的sh
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- 序列化,没有库的整数,得到奇怪的结果
- 使用取消引用的指针的多态性会产生意外的结果.为什么?
- 在更改for循环的第三部分后,未使用for循环结果
- 如何设置一个范围来提取我想要获得的信息
- 使用++运算符会导致意外的结果
- 为什么在逗号分隔符上下文中将预增量的结果强制转换为void
- C++Brute Force攻击函数不会返回结果
- 从 CSV C++中提取双精度值时出现意外结果
- 将SSE转换为霓虹灯:如何打包然后提取32位结果
- 从CPPRest库(即casablanca)获得的结果中提取基本STL字符串