#include <Windows.h>
#include <wininet.h>
#include <iostream>
#include <vector>
#include <string>
#include <string_view>
#pragma comment(lib, "wininet.lib")
std::string read_webpage(const std::string_view url) {
std::string webpage;
HINTERNET internet_handle = InternetOpenA("PageReading", INTERNET_OPEN_TYPE_DIRECT, nullptr, nullptr, 0);
if (internet_handle) {
HINTERNET connect_handle = InternetOpenUrlA(internet_handle, url.data(), nullptr, 0, INTERNET_FLAG_RELOAD, 0);
if (connect_handle != nullptr) {
std::vector<char> buffer(4096);
DWORD bytes_readed;
while (InternetReadFile(connect_handle, buffer.data(), buffer.size(), &bytes_readed)
&& bytes_readed != 0)
{
webpage.append(buffer.data(), bytes_readed);
}
InternetCloseHandle(connect_handle);
}
else {
std::cerr << "Cannot open url: " << url << std::endl;
}
InternetCloseHandle(internet_handle);
}
else {
std::cerr << "Cannot initialize wininet." << std::endl;
}
return webpage;
}
int main()
{
const std::string_view url = "https://pastebin.com/raw/dUPjSWEj";
std::string page = read_webpage(url);
if (!page.empty()) {
std::cout << "Content is: " << page << std::endl;
}
else {
std::cout << "Something wrong, webpage is empty?" << std::endl;
}
return 0;
}