|
1 | 1 | #include <iostream> |
2 | 2 | #include <string> |
| 3 | +#include <curl/curl.h> |
| 4 | + |
| 5 | +size_t WriteCallback(void* contents, size_t size, size_t nmemb, std::string* output) { |
| 6 | + output->append((char*)contents, size * nmemb); |
| 7 | + return size * nmemb; |
| 8 | +} |
3 | 9 |
|
4 | 10 | int main() { |
5 | 11 | std::string url; |
6 | | - |
7 | 12 | std::cout << "Enter a URL: "; |
8 | | - std::getline(std::cin, url); // user can type any URL |
9 | | - |
10 | | - // Remove protocol (http:// or https://) |
11 | | - std::string protocol = "https://"; |
12 | | - size_t start = url.find(protocol); |
13 | | - if (start != std::string::npos) { |
14 | | - start += protocol.length(); |
15 | | - } else { |
16 | | - protocol = "http://"; |
17 | | - start = url.find(protocol); |
18 | | - if (start != std::string::npos) |
19 | | - start += protocol.length(); |
20 | | - else |
21 | | - start = 0; // no protocol found |
| 13 | + std::getline(std::cin, url); |
| 14 | + |
| 15 | + CURL* curl = curl_easy_init(); |
| 16 | + if (!curl) { |
| 17 | + std::cerr << "Failed to initialize curl\n"; |
| 18 | + return 1; |
22 | 19 | } |
23 | 20 |
|
24 | | - // Find the end of the domain |
25 | | - size_t end = url.find('/', start); |
| 21 | + std::string response; |
26 | 22 |
|
27 | | - // Extract the forum/domain name |
28 | | - std::string forumName = url.substr(start, end - start); |
| 23 | + curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); |
| 24 | + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); |
| 25 | + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); |
| 26 | + |
| 27 | + CURLcode res = curl_easy_perform(curl); |
| 28 | + curl_easy_cleanup(curl); |
| 29 | + |
| 30 | + if (res != CURLE_OK) { |
| 31 | + std::cerr << "Request failed\n"; |
| 32 | + return 1; |
| 33 | + } |
29 | 34 |
|
30 | | - std::cout << "Forum name: " << forumName << "\n"; |
| 35 | + std::cout << "Page content:\n" << response << "\n"; |
31 | 36 | } |
0 commit comments