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)
小智 5
您的程序运行结束main并终止。您需要wait在then调用后添加:
client.request(methods::GET).then([](http_response response)
{
// ...
}).wait();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12047 次 |
| 最近记录: |