使用适用于C ++的Google API客户端库的HTTP请求

时间:2015-04-10 18:38:31

标签: c++ http google-api google-api-client

我正在尝试使用Google API客户端库为C ++发送HTTP GET请求,使用http://google.github.io/google-api-cpp-client/latest/guide/making_http_requests.html中提到的用例示例。

这是我的计划:

#include "googleapis/client/data/data_reader.h"
#include "googleapis/client/transport/http_request.h"
#include "googleapis/client/transport/http_response.h"
#include "googleapis/client/transport/http_transport.h"
#include "googleapis/client/transport/curl_http_transport.h"
#include "googleapis/base/scoped_ptr.h"
#include "googleapis/base/mutex.h"
#include <curl/curl.h>
#include <glog/logging.h>
#include "googleapis/util/status.h"
#include <iostream>
#include <cstring>
#include <cstdlib>

using namespace googleapis;
using namespace std;

using googleapis::client::HttpRequest;
using googleapis::client::HttpRequestState;
using googleapis::client::HttpResponse;
using googleapis::client::HttpTransport;
using googleapis::client::HttpTransportLayerConf;
using googleapis::client::HttpTransportOptions;

void IllustrateGet(const char* url, HttpTransport* transport) {
    scoped_ptr<HttpRequest> request(
            transport->NewHttpRequest(HttpRequest::GET));
    request->set_url(url);
    util::Status status = request->Execute();
    if (!status.ok())
        cerr << status.error_message();
}


int main(){

    string url = "http://www.google.com/cloudprint";
    scoped_ptr<HttpTransport> transport;

    IllustrateGet(url, transport);
    return 0;

}

在main()中,当我尝试调用IllustrateGet函数时,我得到一个无效的参数异常。有人可以帮我理解HttpTransport为了发送HTTP GET请求会做什么吗?

1 个答案:

答案 0 :(得分:1)

调用IllustrateGet(url.c_str(),transport);