Created
April 3, 2026 05:27
-
-
Save sunmeat/ffb3c5b99a1a2ec337efd0f1cf9d062b to your computer and use it in GitHub Desktop.
get html page text C++ example HTTP GET mac os version
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <string> | |
| #include <cstring> | |
| #include <sys/socket.h> | |
| #include <netdb.h> | |
| #include <unistd.h> | |
| using namespace std; | |
| int main() { | |
| string url = "google.com"; | |
| string get_http = "GET / HTTP/1.1\r\nHost: " + url + "\r\nConnection: close\r\n\r\n"; | |
| // резолв DNS | |
| struct hostent* host = gethostbyname(url.c_str()); | |
| if (!host) { | |
| cerr << "Could not resolve host\n"; | |
| return 1; | |
| } | |
| // створення сокету | |
| int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); | |
| if (sock < 0) { | |
| cerr << "Could not create socket\n"; | |
| return 1; | |
| } | |
| struct sockaddr_in sockAddr; | |
| sockAddr.sin_port = htons(80); | |
| sockAddr.sin_family = AF_INET; | |
| sockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr); | |
| if (connect(sock, (struct sockaddr*)&sockAddr, sizeof(sockAddr)) != 0) { | |
| cerr << "Could not connect\n"; | |
| return 2; | |
| } | |
| // надсилання GET-запиту | |
| send(sock, get_http.c_str(), get_http.size(), 0); | |
| // отримання відповіді | |
| char buffer[10000]; | |
| int nDataLength; | |
| string website_HTML; | |
| while ((nDataLength = recv(sock, buffer, sizeof(buffer), 0)) > 0) { | |
| int i = 0; | |
| while (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r') { | |
| website_HTML += buffer[i]; | |
| i++; | |
| } | |
| } | |
| close(sock); // замість closesocket + WSACleanup | |
| cout << website_HTML; | |
| cout << "\n\nPress ENTER to close.\n\n"; | |
| cin.ignore(); | |
| cin.get(); | |
| return 0; | |
| } | |
| =================================================================================================== | |
| #include <iostream> | |
| #include <fstream> | |
| #include <string> | |
| #include <curl/curl.h> | |
| using namespace std; | |
| // сallback для запису даних у файл | |
| size_t write_data(void* ptr, size_t size, size_t nmemb, FILE* stream) { | |
| return fwrite(ptr, size, nmemb, stream); | |
| } | |
| int main() { | |
| string srcURL = "https://otu.edu.ua/"; | |
| string destFile = "/tmp/otu.html"; // macOS: /tmp замість C:/1/ | |
| CURL* curl = curl_easy_init(); | |
| if (!curl) { | |
| cerr << "Failed to init curl\n"; | |
| return 1; | |
| } | |
| FILE* f = fopen(destFile.c_str(), "wb"); | |
| if (!f) { | |
| cerr << "Cannot open file for writing\n"; | |
| return 1; | |
| } | |
| curl_easy_setopt(curl, CURLOPT_URL, srcURL.c_str()); | |
| curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); | |
| curl_easy_setopt(curl, CURLOPT_WRITEDATA, f); | |
| curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); // слідувати редиректам | |
| CURLcode res = curl_easy_perform(curl); | |
| fclose(f); | |
| curl_easy_cleanup(curl); | |
| if (res == CURLE_OK) { | |
| cout << "Saved to " << destFile << "\n\n"; | |
| } else { | |
| cerr << "Download failed: " << curl_easy_strerror(res) << "\n"; | |
| return 1; | |
| } | |
| // читання та виведення файлу | |
| // (на macOS термінал вже UTF-8, SetConsoleOutputCP не потрібен) | |
| f = fopen(destFile.c_str(), "r"); | |
| if (!f) { | |
| cerr << "Cannot open file for reading\n"; | |
| return 1; | |
| } | |
| char buffer[200]; | |
| while (true) { | |
| fgets(buffer, 199, f); | |
| if (feof(f) == 0) | |
| cout << buffer; | |
| else | |
| break; | |
| } | |
| fclose(f); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment