简体   繁体   中英

c++ Http request SSL error

OK so I'm working on this c++ project where I read from a site, needs to be google, to get answers to display to the user and I am using the code from this link calling a website second solution the second solution as the base for my project. Requesting the xml from Google's search results will not work as the answers are not in the xml so I made a google custom search to output to a json using the google custom search api. My problem is when is requesting the url, link to my google custom search example here , of the json it gives an ssl error from my program. How do I solve this ssl problem? Seems I need to GET https not http or I need code to verify the ssl certificate. Not sure how to do this as I'm new to protocols and networking and such on c++.

Cannot use third part libraries.

And I apologize ahead of time for my problem and being a noob on this subject.

The given code uses plain sockets, no SSL is mentioned in the code, so it will not work with HTTPS or any other SSL based protocol. In order to connect to HTTPS web page, one has to use OpenSSL or any other library providing this layer for TCP sockets (unless you're gonna deal with encryption on your own which I doubt). Here is an example by using Boost library:

/*

Compile with
    g++ -std=c++11 -lpthread -lboost_system -lssl -lcrypto -ogoog goog.cpp

*/


#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>


using std::cout;
using std::endl;
using std::vector;
using std::string;
using boost::asio::ip::tcp;
using boost::asio::ip::address;
using boost::asio::io_service;
using boost::asio::connect;
using boost::asio::buffer;
using boost::system::error_code;
using boost::system::system_error;
using boost::asio::ssl::context;


int main()
{
    const char* PORT = "443";
    const string HOST = "googleapis.com";
    const string REQUEST = "GET https://www.googleapis.com/customsearch/v1?q=when%20is%20george%20washingtons%20birthdate&cx=014855184903748195002:umdboiolvoi&key=AIzaSyDxFosFrZlMpgdFeTsPWZfp925MbaBX49s HTTP/1.1\r\n\r\n";

    try
    {
        io_service ios;
        tcp::resolver resolver(ios);
        tcp::resolver::query query(HOST, PORT);
        tcp::resolver::iterator iterator = resolver.resolve(query);
        context ctx(context::sslv23);

        boost::asio::ssl::stream<tcp::socket> sock(ios, ctx);
        sock.set_verify_mode(boost::asio::ssl::verify_none);
        connect(sock.lowest_layer(), iterator);
        sock.handshake(boost::asio::ssl::stream_base::client);

        const int BUFLEN = 2048;
        vector<char> buf(BUFLEN);
        sock.write_some(boost::asio::buffer(REQUEST, REQUEST.size()));
        while (true)
        {
            size_t len = sock.read_some(boost::asio::buffer(buf, BUFLEN));
            cout << "main(): buf.data()=";
            cout.write(buf.data(), len);
        }

    }
    catch (system_error& exc)
    {
        cout << "main(): exc.what()=" << exc.what() << endl;
    }

    return EXIT_SUCCESS;
}

It connects to Google APIs (no certificate verification is performed) over SSL socket, sends GET request, fetches the page and prints to stdout. However, it is done in the infinite loop, so it's up to you to parse the JSON answer and determine when to exit the reading loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM