C++;REST SDK卡萨布兰卡客户端.request 我想编写一个C++程序,它向服务器发送请求,获取一些数据。我找到了C++休眠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; } #包括 #包括 #包括 #包括 #包括“cpprest/containerstream.h” #包括“cpprest/filestream.h” #包括“cpprest/http_client.h” #包括“cpprest/json.h” #包括“cpprest/producerconsumerstream.h” #包括“cpprest/http_client.h” #包括 #包括 使用名称空间std; 使用命名空间web; 使用名称空间web::json; 使用名称空间web::http; 使用名称空间web::http::client; 使用名称空间实用程序; 使用名称空间实用工具::转换; int main(){ http_客户端(L)http://httpbin.org/ip"); client.request(methods::GET).then([](http\U响应) { if(response.status_code()==status_code::OK) { auto body=response.extract_string().get(); std::wcout
主线程可能在“请求”任务完成之前终止,因此您无法看到任何控制台输出。我建议您在完成后调用任务“wait()”函数。然后,如上的答案所示,您的程序从C++;REST SDK卡萨布兰卡客户端.request 我想编写一个C++程序,它向服务器发送请求,获取一些数据。我找到了C++休眠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; } #包括 #包括 #包括 #包括 #包括“cpprest/containerstream.h” #包括“cpprest/filestream.h” #包括“cpprest/http_client.h” #包括“cpprest/json.h” #包括“cpprest/producerconsumerstream.h” #包括“cpprest/http_client.h” #包括 #包括 使用名称空间std; 使用命名空间web; 使用名称空间web::json; 使用名称空间web::http; 使用名称空间web::http::client; 使用名称空间实用程序; 使用名称空间实用工具::转换; int main(){ http_客户端(L)http://httpbin.org/ip"); client.request(methods::GET).then([](http\U响应) { if(response.status_code()==status_code::OK) { auto body=response.extract_string().get(); std::wcout,c++,rest,request,client,casablanca,C++,Rest,Request,Client,Casablanca,主线程可能在“请求”任务完成之前终止,因此您无法看到任何控制台输出。我建议您在完成后调用任务“wait()”函数。然后,如上的答案所示,您的程序从main的末尾运行并终止。您需要在之后添加wait,然后调用: client.request(methods::GET).then([](http_response response) { // ... }).wait(); 此代码正在运行: // ConsoleApplication1.cpp : Defines the entry po
main
的末尾运行并终止。您需要在之后添加wait
,然后调用:
client.request(methods::GET).then([](http_response response)
{
// ...
}).wait();
此代码正在运行:
// ConsoleApplication1.cpp : Defines the entry point for the console application.
#include "StdAfx.h"
#include <cpprest/http_client.h>
#include <cpprest/filestream.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
int main(int argc, char* argv[])
{
// Make the request and asynchronously process the response.
http_client client(L"http://localhost:8082/TPJAXRS/Test/test");
client.request(methods::GET).then([](http_response response){
if(response.status_code() == status_codes::OK){
auto body = response.extract_string().get();
std::wcout << body<< std::endl;
}});
std::cout << "Hello world!" << std::endl;
system("PAUSE");
return 0;
}
//ConsoleApplication1.cpp:定义控制台应用程序的入口点。
#包括“StdAfx.h”
#包括
#包括
使用命名空间实用程序;//常用实用程序,如字符串转换
使用命名空间web;//URI等常见功能。
使用命名空间web::http;//通用http功能
使用命名空间web::http::client;//http客户端功能
使用命名空间并发::streams;//异步流
int main(int argc,char*argv[])
{
//发出请求并异步处理响应。
http_客户端(L)http://localhost:8082/TPJAXRS/Test/test");
client.request(methods::GET).then([](http_响应){
if(response.status_code()==status_code::OK){
auto body=response.extract_string().get();
标准::wcout#包括
#包括
#包含//HTTP服务器
#include//JSON库
#包含//URI库
#包括//WebSocket客户端
#包含//STL容器支持的异步流
#包括//用于将异步流与STL和WinRT流集成的桥
#包含//由指向内存的原始指针支持的异步流
#包括//生产者-消费者场景的异步流
使用命名空间实用程序;//常用实用程序,如字符串转换
使用命名空间web;//URI等常见功能。
使用命名空间web::http;//通用http功能
使用命名空间web::http::client;//http客户端功能
使用命名空间并发::streams;//异步流
使用命名空间web::http::experimental::listener;//http服务器
使用命名空间web::experimental::web_sockets::client;//WebSockets client
使用命名空间web::json;//json库
int main(int argc,char*argv[])
{
auto fileStream=std::make_shared();
//打开流以输出文件。
pplx::task requestTask=fstream::open_ostream(U(“results.html”))。然后([=](ostream outFile)
{
*fileStream=outFile;
//创建http_客户端以发送请求。
http_客户端(U)http://www.bing.com/"));
//生成请求URI并启动请求。
uri_生成器(U(“/search”);
附加查询(U(“q”)、U(“cpprestsdk github”);
返回client.request(方法::GET,builder.to_string());
})
//处理到达的响应头。
。然后([=](http_响应)
{
printf(“收到的响应状态代码:%u\n”,response.status_code());
//将响应主体写入文件。
返回response.body();
})
//关闭文件流。
。然后([=](大小)
{
返回fileStream->close();
});
//等待所有未完成的I/O完成并处理任何异常
尝试
{
requestTask.wait();
}
捕获(const std::exception&e)
{
printf(“错误异常:%s\n”,例如what());
}
返回0;
}
这是用于在HTTP请求中设置的
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include <cpprest/http_listener.h> // HTTP server
#include <cpprest/json.h> // JSON library
#include <cpprest/uri.h> // URI library
#include <cpprest/ws_client.h> // WebSocket client
#include <cpprest/containerstream.h> // Async streams backed by STL containers
#include <cpprest/interopstream.h> // Bridges for integrating Async streams with STL and WinRT streams
#include <cpprest/rawptrstream.h> // Async streams backed by raw pointer to memory
#include <cpprest/producerconsumerstream.h> // Async streams for producer consumer scenarios
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::experimental::listener; // HTTP server
using namespace web::experimental::web_sockets::client; // WebSockets client
using namespace web::json; // JSON library
int main(int argc, char* argv[])
{
auto fileStream = std::make_shared<ostream>();
// Open stream to output file.
pplx::task<void> requestTask = fstream::open_ostream(U("results.html")).then([=](ostream outFile)
{
*fileStream = outFile;
// Create http_client to send the request.
http_client client(U("http://www.bing.com/"));
// Build request URI and start the request.
uri_builder builder(U("/search"));
builder.append_query(U("q"), U("cpprestsdk github"));
return client.request(methods::GET, builder.to_string());
})
// Handle response headers arriving.
.then([=](http_response response)
{
printf("Received response status code:%u\n", response.status_code());
// Write response body into the file.
return response.body().read_to_end(fileStream->streambuf());
})
// Close the file stream.
.then([=](size_t)
{
return fileStream->close();
});
// Wait for all the outstanding I/O to complete and handle any exceptions
try
{
requestTask.wait();
}
catch (const std::exception &e)
{
printf("Error exception:%s\n", e.what());
}
return 0;
}