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.

37 lines
1.1 KiB

  1. #include <curlpp/Easy.hpp>
  2. #include <curlpp/Options.hpp>
  3. #include <curlpp/cURLpp.hpp>
  4. #include <curlpp/Infos.hpp>
  5. #include <iostream>
  6. #include <sstream>
  7. #include <string>
  8. int main(int argc, char *argv[]) {
  9. try {
  10. std::string url = "https://www.example.com";
  11. curlpp::Cleanup cleaner;
  12. curlpp::Easy request;
  13. // 设置请求的 URL
  14. request.setOpt(new curlpp::options::Url(url));
  15. // 将响应数据存储到一个字符串中
  16. std::ostringstream response;
  17. request.setOpt(new curlpp::options::WriteStream(&response));
  18. // 执行请求并检查返回状态码
  19. request.perform();
  20. long http_code = curlpp::infos::ResponseCode::get(request);
  21. if (http_code == 200) {
  22. std::cout << "Response: " << response.str() << std::endl;
  23. } else {
  24. std::cerr << "Error: HTTP status code " << http_code << std::endl;
  25. }
  26. } catch (curlpp::RuntimeError &e) {
  27. std::cerr << "Error: " << e.what() << std::endl;
  28. return 1;
  29. } catch (curlpp::LogicError &e) {
  30. std::cerr << "Error: " << e.what() << std::endl;
  31. return 1;
  32. }
  33. return 0;
  34. }