2010-11-19 396 views
7

是否有任何開源庫可用於C++中實現RESTful客戶端(用於解釋HTTP請求作爲REST服務調用的庫)?C++中的RESTful客戶端API

我的要求是連接到亞馬遜網絡服務,並獲取可用於C++中給定用戶帳戶的EC2實例(及其詳細信息)的列表。

我知道亞馬遜在Java,C#中爲此提供了API。但我想用C++。如果亞馬遜也提供C++,那就沒問題了,請指導我。

非常感謝您的幫助。

Regards

Bharatha Selvan。

回答

2

您需要解析XML。我建議你試試Qt C++ Toolkit,它會給你一個QHttp實例來進行HTTP調用和QtXml模塊來解析xml。這樣你可以創建一個C++ Rest客戶端。

2

您應該嘗試ffead-cpp網絡框架。它具有許多其他功能,如依賴注入,序列化,有限反射,JSON等等。請檢查...

0

Restbed提供同步和異步HTTP/HTTPS客戶端功能。

#include <memory> 
#include <future> 
#include <cstdio> 
#include <cstdlib> 
#include <restbed> 

using namespace std; 
using namespace restbed; 

void print(const shared_ptr<Response>& response) 
{ 
    fprintf(stderr, "*** Response ***\n"); 
    fprintf(stderr, "Status Code: %i\n", response->get_status_code()); 
    fprintf(stderr, "Status Message: %s\n", response->get_status_message().data()); 
    fprintf(stderr, "HTTP Version: %.1f\n", response->get_version()); 
    fprintf(stderr, "HTTP Protocol: %s\n", response->get_protocol().data()); 

    for (const auto header : response->get_headers()) 
    { 
     fprintf(stderr, "Header '%s' > '%s'\n", header.first.data(), header.second.data()); 
    } 

    auto length = 0; 
    response->get_header("Content-Length", length); 

    Http::fetch(length, response); 

    fprintf(stderr, "Body:   %.*s...\n\n", 25, response->get_body().data()); 
} 

int main(const int, const char**) 
{ 
    auto request = make_shared<Request>(Uri("http://www.corvusoft.co.uk:80/?query=search%20term")); 
    request->set_header("Accept", "*/*"); 
    request->set_header("Host", "www.corvusoft.co.uk"); 

    auto response = Http::sync(request); 
    print(response); 

    auto future = Http::async(request, [ ](const shared_ptr<Request>, const shared_ptr<Response> response) 
    { 
     fprintf(stderr, "Printing async response\n"); 
     print(response); 
    }); 

    future.wait(); 

    return EXIT_SUCCESS; 
}