Advertisement
Guest User

rekt

a guest
Nov 21st, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <curl/curl.h>
  3. #include <stdexcept>
  4.  
  5. class ClientInitExcept : public std::runtime_error
  6. {
  7.     public:
  8.         ClientInitExcept() : std::runtime_error("Client init failed") {}
  9. };
  10.  
  11. class ClientRequestExcept : public std::runtime_error
  12. {
  13.     public:
  14.         ClientRequestExcept() : std::runtime_error("Client request failed") {}
  15. };
  16.  
  17. class Client
  18. {
  19.     CURL *curl;
  20.  
  21.     public:
  22.     Client();
  23.     ~Client();
  24.     int get(std::string);
  25. };
  26.  
  27. Client::Client()
  28. {
  29.     CURLcode rv = curl_global_init(CURL_GLOBAL_SSL);
  30.     if (rv != 0)
  31.         throw ClientInitExcept();
  32.  
  33.     curl = curl_easy_init();
  34.     if (!curl)
  35.         throw ClientInitExcept();
  36. }
  37.  
  38. Client::~Client()
  39. {
  40.     curl_easy_cleanup(curl);
  41.     curl_global_cleanup();
  42. }
  43.  
  44. int Client::get(std::string url)
  45. {
  46.     curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  47.     curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
  48.     curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 1L);
  49.  
  50.     CURLcode res = curl_easy_perform(curl);
  51.     if (res != CURLE_OK)
  52.         throw ClientRequestExcept();
  53.  
  54.     return res;
  55. }
  56.  
  57. int main(int argc, char **argv)
  58. {
  59.     if (argc < 2)
  60.         return EXIT_FAILURE;
  61.  
  62.     try
  63.     {
  64.         Client client;
  65.         client.get(argv[1]);
  66.     }
  67.     catch (std::exception &e)
  68.     {
  69.         std::cerr << e.what() << std::endl;
  70.         return EXIT_FAILURE;
  71.     }
  72.  
  73.     return EXIT_SUCCESS;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement