在C++中,可以使用第三方库来发送和接收HTTP请求,比如curlcpp、cpprestsdk等
在C++中,可以使用第三方库来发送和接收HTTP请求,比如curlcpp、cpprestsdk等。下面是一个使用cpprestsdk库发送HTTPGET请求的示例代码:
#include<cpprest/http_client.h>
#include<cpprest/filestream.h>
usingnamespaceweb;
usingnamespaceweb::http;
usingnamespaceweb::http::client;
intmain(){
//创建一个http_client对象
http_clientclient(U("http://www.example.com"));
//发送一个GET请求
client.request(methods::GET).then([](http_responseresponse){
if(response.status_code()==status_codes::OK){
//将响应消息主体保存到文件中
concurrency::streams::ofstream::open_ostream(U("response.txt")).then([=](concurrency::streams::ostreamoutput){
returnresponse.body().read_to_end(output.streambuf());
}).then([=](size_t){
//读取完成
std::wcout<<L"Filesaved"<<std::endl;
}).wait();
}
}).wait();
return0;
}
以上代码使用cpprestsdk库发送一个HTTPGET请求到http://www.example.com,并将响应消息主体保存到response.txt文件中。你可以根据需要修改代码来发送不同类型的HTTP请求和处理响应。
版权声明
本文仅代表作者观点,不代表博信信息网立场。