Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
256 views
in Technique[技术] by (71.8m points)

c++ - How to get google elevation api with boost library?

*Environment C++ 11 Boost 1.73

I followed https://www.boost.org/doc/libs/1_70_0/libs/beast/doc/html/beast/quick_start/http_client.html example.

It was success when I got the data from my own Http Server.(I mean local 127.0.0.1)

But I couldn't get the data from google elevation api with this example.

my "get" function is.

json HttpClient::GET(std::string host, std::string port, std::string target)
{
    njson result = njson::object();
    result["state"] = "F";

    try{
        // The io_context is required for all I/O
        net::io_context ioc;

        // These objects perform our I/O
        tcp::resolver resolver(ioc);
        beast::tcp_stream stream(ioc);

        // Look up the domain name
    
        auto const results = resolver.resolve(host, port);

        // Make the connection on the IP address we get from a lookup
        stream.connect(results);

        // Set up an HTTP GET request message
        http::request<http::string_body> req{http::verb::get, target, HTTP_VERSION };
        req.set(http::field::host, host);
        req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);

        // Send the HTTP request to the remote host
        http::write(stream, req);

        // This buffer is used for reading and must be persisted
        beast::flat_buffer buffer;

        // Declare a container to hold the response
        http::response<http::dynamic_body> res;

        // Receive the HTTP response
        http::read(stream, buffer, res);

        // Write the message to standard out
        std::string sstr = boost::beast::buffers_to_string(res.body().data());
        
        result = njson::parse(sstr);
    
        // Gracefully close the socket
        beast::error_code ec;
        stream.socket().shutdown(tcp::socket::shutdown_both, ec);

        // not_connected happens sometimes
        // so don't bother reporting it.
        //
        if (ec && ec != beast::errc::not_connected)
            throw beast::system_error{ ec };

        // If we get here then the connection is closed gracefully
    }
    catch (std::exception const& e)
    {
        std::cerr << "Error: " << e.what() << std::endl;
    }
    return result;
}

That is my main

// Performs an HTTP GET and prints the response
int main(int argc, char** argv)
{
    auto const host = "https://maps.googleapis.com";
    auto const port = "443";
    auto const target = "/maps/api/elevation/json?locations=39.7391536,-104.9847034&key=MY_KEY";

    json res = HttpClient::GET(host,port, target);
    std::cout << "***********************" << std::endl;
    std::cout << res.dump() << std::endl;
    return 0;
}

but the result is. Resolve: no such host is known

When I make that code, it go to catch statement.

auto const results = resolver.resolve(host, port);

I think one of my params is wrong. But I don't have any idea. what should I do?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Remove "https://" in hostName. It worked fine. but... I have an "ERROR : end of stream" message now.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...