我正在编写一些代码,我需要执行 GET 请求并处理收到的信息。为此,我正在为请求使用 C++ REST SDK(代号“Casablanca”)
这是我的代码
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
using namespace utility;
using namespace web;
using namespace web::http;
using namespace web::http::client;
using namespace concurrency::streams;
//This method i saw on the Microsoft documentation
pplx::task<void> HTTPStreamingAsync()
{
http_client client(L"http://localhost:10000/Something"); //The api is running at the moment
// Make the request and asynchronously process the response.
return client.request(methods::GET).then([](http_response response)
{
// Print the status code.
std::wostringstream ss;
ss << L"Server returned returned status code " << response.status_code() << L'.' << std::endl;
std::wcout << ss.str();
// TODO: Perform actions here reading from the response stream.
auto bodyStream = response.body();
// In this example, we print the length of the response to the console.
ss.str(std::wstring());
ss << L"Content length is " << response.headers().content_length() << L" bytes." << std::endl;
std::wcout << ss.str();
});
}
void main(int argc, char **argv)
{
HTTPStreamingAsync().wait();
//...
}
当我使用调试时,我在以下行中收到错误:
返回 client.request(methods::GET).then([](http_response 响应)
通过调试,我看到变量“client”有内容,但我仍然收到此错误:
我用谷歌搜索错误,大多数人说这是代码错误(试图访问内存的某些部分)...
有什么想法吗?
最佳答案
当 cpprestsdk DLL 使用Multi-Threaded DLL
/MD
构建并且调用库使用Multi-Threaded
构建时,可能会发生此问题> /MT
。由于 cpprestsdk 不提供 .lib 文件的配置,您不得不使用 /MD
。至少据我所知这是最好的,因为我无法在没有一堆链接器错误的情况下开箱即用地编译 cpprestsdk.lib。
关于c++调试断言HTTP请求失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41486307/