Надсилання HTTP-запиту

C++:
Надсилання HTTP-запиту

How to: / Як це зробити:

#include <iostream>
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>

int main() {
    auto fileStream = std::make_shared<concurrency::streams::ostream>();

    // Відкрийте потік для запису у файл
    concurrency::streams::fstream::open_ostream(U("result.html")).then(
        [fileStream](concurrency::streams::ostream outFile) {
            *fileStream = outFile;

            // Створіть HTTP-клієнта
            web::http::client::http_client client(U("http://example.com"));

            // Відправте запит GET
            return client.request(web::http::methods::GET);
        }
    ).then([fileStream](web::http::http_response response) {
        // Направте відповідь у потік файлу
        return response.body().read_to_end(fileStream->streambuf());
    }).then([fileStream](size_t) {
        return fileStream->close();
    }).wait();

    std::cout << "HTTP request sent. Check 'result.html' for response." << std::endl;

    return 0;
}

Sample output:

HTTP request sent. Check 'result.html' for response.

Deep Dive / Заглиблення:

Sending an HTTP request in C++ usually involves leveraging libraries because the standard C++ library doesn’t provide this functionality directly. Historically, programmers often used libcurl, but C++ now has the cpprestsdk (formerly known as Casablanca) which simplifies these tasks. Alternatives to cpprestsdk include Boost.Beast or POCO libraries. When implementing an HTTP request, consider the type (GET, POST, etc.), error handling, resource cleanup, and platform-specific behavior.

See Also / Дивіться також: