lundi 28 novembre 2022

asio HTTPS request response body is gibberish

i'm trying to send a get request to get some information, and I am using asio and a https request and ssl. When I send the request, I get a fully formed header, but the response is just non printable unicode characters. Despite the response not being readable, it has the same amount of characters as what the response is supposed have. Below is my code, and the response I get.

#include <string>
#include <iostream>
#include <chrono>

#include "asio.hpp"
#include "asio/ssl.hpp"

using namespace std;

float getTickerValue(const string& ticker)
{
    float tickerPrice;
    string host = "query1.finance.yahoo.com";
    string portNumber = "80";

    typedef asio::ssl::stream<asio::ip::tcp::socket> ssl_socket;

    // Create a context that uses the default paths for
    // finding CA certificates.
    asio::ssl::context ctx(asio::ssl::context::sslv23);
    ctx.set_default_verify_paths();

    // Open a socket and connect it to the remote host.
    asio::io_service io_service;
    ssl_socket sock(io_service, ctx);
    asio::ip::tcp::resolver resolver(io_service);
    asio::ip::tcp::resolver::query query(host, "https");
    asio::connect(sock.lowest_layer(), resolver.resolve(query));
    sock.lowest_layer().set_option(asio::ip::tcp::no_delay(true));

    // Perform SSL handshake and verify the remote host's
    // certificate.
    sock.set_verify_mode(asio::ssl::verify_peer);
    sock.set_verify_callback(asio::ssl::rfc2818_verification(host));
    sock.handshake(ssl_socket::client);

    // ... read and write as normal ...
    {
        string request = "GET /v11/finance/quoteSummary/" + ticker + "?modules=financialData HTTP/1.1\r\n"
                                                                     "Host: " + host + "\r\n"
                                                                     "Connection: close\r\n\r\n";
        sock.write_some(asio::buffer(request.data(), request.size()));

        using namespace std::chrono_literals;
        std::this_thread::sleep_for(200ms);  // THIS IS TEMPORARY

        size_t bytes = sock.lowest_layer().available();
        cout << "Bytes avalible: " << bytes << endl;

        if (bytes > 0) {
            vector<char> vBuffer(bytes);
            sock.read_some(asio::buffer(vBuffer.data(), vBuffer.size()));
            for (auto c: vBuffer) {
                cout << c;
            }
        }
    }
    return tickerPrice;
}

response from code

Thanks!

Aucun commentaire:

Enregistrer un commentaire