C++ REST SDK Casablanca Client.request

use*_*493 2 c++ rest client request casablanca

我想编写一个小 C++ 程序,向服务器发送请求并获取一些数据。我找到了 C++ Rest-SDK 并决定使用它。我在不同的网站上搜索了代码示例,但其中许多都不起作用并显示语法错误。我现在得到的是该代码,但跳过了 client.request 方法。该程序永远不会跳进来。希望有人能意识到这个问题,也许可以解释我必须改变的地方。

#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;
}
Run Code Online (Sandbox Code Playgroud)

the*_*oon 5

主线程可能在“请求”任务完成之前终止,因此您看不到任何控制台输出。我建议你在“.then”之后调用任务“wait()”函数,就像他们网站上的答案一样


小智 5

您的程序运行结束main并终止。您需要waitthen调用后添加:

client.request(methods::GET).then([](http_response response)
{ 
    // ...
}).wait();
Run Code Online (Sandbox Code Playgroud)