You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

38 lines
1.1 KiB

#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>
#include <curlpp/cURLpp.hpp>
#include <curlpp/Infos.hpp>
#include <iostream>
#include <sstream>
#include <string>
int main(int argc, char *argv[]) {
try {
std::string url = "https://www.example.com";
curlpp::Cleanup cleaner;
curlpp::Easy request;
// 设置请求的 URL
request.setOpt(new curlpp::options::Url(url));
// 将响应数据存储到一个字符串中
std::ostringstream response;
request.setOpt(new curlpp::options::WriteStream(&response));
// 执行请求并检查返回状态码
request.perform();
long http_code = curlpp::infos::ResponseCode::get(request);
if (http_code == 200) {
std::cout << "Response: " << response.str() << std::endl;
} else {
std::cerr << "Error: HTTP status code " << http_code << std::endl;
}
} catch (curlpp::RuntimeError &e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1;
} catch (curlpp::LogicError &e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
return 0;
}