C ++ REST SDK卡萨布兰卡Client.request [英] C++ REST SDK Casablanca Client.request
问题描述
我想编写一个小c ++程序,该程序将请求发送到服务器并获取一些数据。我找到了C ++ Rest-SDK并决定使用它。我在不同的网站上搜索了代码示例,但其中许多都无法正常运行,并且显示语法错误。我现在得到的是该代码,但是跳过了client.request方法。该程序永远不会跳进来。希望有人能意识到问题并可能解释我必须更改的内容。
I want to write a little c++ program that sends a request to a server an get some data. I found the C++ Rest-SDK and decided to use it. I searched on different websites for code-examples but many of them doesn't work an shows syntax errors. What i got now is that code but the client.request method is skipped. The program never jumps in. Hope someone can realise the problem and maybe explain what i have to change.
#include <Windows.h>
#include <iostream>
#include <sstream>
#include <string>
#include "cpprest/containerstream.h"
#include "cpprest/filestream.h"
#include "cpprest/http_client.h"
#include "cpprest/json.h"
#include "cpprest/producerconsumerstream.h"
#include "cpprest/http_client.h"
#include <string.h>
#include <conio.h>
using namespace std;
using namespace web;
using namespace web::json;
using namespace web::http;
using namespace web::http::client;
using namespace utility;
using namespace utility::conversions;
int main() {
http_client client(L"http://httpbin.org/ip");
client.request(methods::GET).then([](http_response response)
{
if(response.status_code() == status_codes::OK)
{
auto body = response.extract_string().get();
std::wcout << body;
getch();
}
});
return 0;
}
推荐答案
您的程序运行到最后 main
并终止。您需要在 then
调用后添加 wait
:
Your program runs off the end of main
and terminates. You need to add wait
after the then
call:
client.request(methods::GET).then([](http_response response)
{
// ...
}).wait();
这篇关于C ++ REST SDK卡萨布兰卡Client.request的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!