Исходник Простой HTTP запрос через WinAPI и обработка JSON

g305noobo

Известный
Автор темы
Модератор
305
477
для себя сделал, мб кому-то понадобится 🤔


C++:
#include <windows.h>
#include <wininet.h>
#include <iostream>
#include "nlohmann/json.hpp"

int main() {
    std::cout << "Initializing WinINet..." << std::endl;
    HINTERNET hInternet = InternetOpen(L"HTTPGET", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);

    if (hInternet == NULL) {
        std::cout << "Error initializing WinINet" << std::endl;
        return 1;
    }

    std::cout << "Opening URL..." << std::endl;
    HINTERNET hConnect = InternetOpenUrl(hInternet, L"request link here", NULL, 0, INTERNET_FLAG_RELOAD, 0);

    if (hConnect == NULL) {
        std::cout << "Error opening URL" << std::endl;
        InternetCloseHandle(hInternet);
        return 1;
    }

    DWORD bytesRead;
    std::string response_data;
    char buffer[4096];

    while (InternetReadFile(hConnect, buffer, sizeof(buffer), &bytesRead) && bytesRead > 0) {
        response_data.append(buffer, bytesRead);
    }

    std::cout << "Closing connections..." << std::endl;
    InternetCloseHandle(hConnect);
    InternetCloseHandle(hInternet);

    nlohmann::json json_response;
    try {
        std::cout << "Parsing JSON..." << std::endl;
        json_response = nlohmann::json::parse(response_data);
    }
    catch (const std::exception& e) {
        std::cout << "Error parsing JSON: " << e.what() << std::endl;
        return 1;
    }

    // хаваем и перевариваем наш json

    return 0;
}
 

Digger Man52

52NGG
Проверенный
1,122
989
Такое уж лучше в классе делать….
C++:
class WebClient {
private:
    HINTERNET hInternet;
    HINTERNET hConnect;

public:
    bool initialize() {
        std::cout << "Initializing WinINet..." << std::endl;
        hInternet = InternetOpen(L"HTTPGET", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);

        if (hInternet == NULL) {
            std::cout << "Error initializing WinINet" << std::endl;
            return false;
        }

        return true;
    }

    bool openUrl(const std::wstring& url) {
        std::cout << "Opening URL: " << url << std::endl;
        hConnect = InternetOpenUrl(hInternet, url.c_str(), NULL, 0, INTERNET_FLAG_RELOAD, 0);

        if (hConnect == NULL) {
            std::cout << "Error opening URL" << std::endl;
            InternetCloseHandle(hInternet);
            return false;
        }

        return true;
    }

    std::string readResponse() {
        DWORD bytesRead;
        std::string response_data;
        char buffer[4096];

        while (InternetReadFile(hConnect, buffer, sizeof(buffer), &bytesRead) && bytesRead > 0) {
            response_data.append(buffer, bytesRead);
        }

        std::cout << "Closing connections..." << std::endl;
        InternetCloseHandle(hConnect);
        InternetCloseHandle(hInternet);

        return response_data;
    }

    bool parseJson(const std::string& json_data, nlohmann::json& json_response) {
        try {
            std::cout << "Parsing JSON..." << std::endl;
            json_response = nlohmann::json::parse(json_data);
        }
        catch (const std::exception& e) {
            std::cout << "Error parsing JSON: " << e.what() << std::endl;
            return false;
        }

        return true;
    }
};

C++:
int main() {
    WebClient client;
    if (!client.initialize()) {
        return 1;
    }

    if (!client.openUrl(L"ссылка")) {
        return 1;
    }

    std::string response_data = client.readResponse();

    nlohmann::json json_response;
    if (!client.parseJson(response_data, json_response)) {
        return 1;
    }

    // жсон

    return 0;
}