Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <curl/curl.h>
- #include <stdexcept>
- class ClientInitExcept : public std::runtime_error
- {
- public:
- ClientInitExcept() : std::runtime_error("Client init failed") {}
- };
- class ClientRequestExcept : public std::runtime_error
- {
- public:
- ClientRequestExcept() : std::runtime_error("Client request failed") {}
- };
- class Client
- {
- CURL *curl;
- public:
- Client();
- ~Client();
- int get(std::string);
- };
- Client::Client()
- {
- CURLcode rv = curl_global_init(CURL_GLOBAL_SSL);
- if (rv != 0)
- throw ClientInitExcept();
- curl = curl_easy_init();
- if (!curl)
- throw ClientInitExcept();
- }
- Client::~Client()
- {
- curl_easy_cleanup(curl);
- curl_global_cleanup();
- }
- int Client::get(std::string url)
- {
- curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 1L);
- CURLcode res = curl_easy_perform(curl);
- if (res != CURLE_OK)
- throw ClientRequestExcept();
- return res;
- }
- int main(int argc, char **argv)
- {
- if (argc < 2)
- return EXIT_FAILURE;
- try
- {
- Client client;
- client.get(argv[1]);
- }
- catch (std::exception &e)
- {
- std::cerr << e.what() << std::endl;
- return EXIT_FAILURE;
- }
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement