2012-10-25 46 views
4

我試圖使用cpp-netlib來執行異步http請求。我在文檔中找不到任何這樣的例子,因此甚至無法編譯它。我目前的嘗試是低於(在評論中有編譯錯誤)。任何提示如何使其工作?先謝謝你!示例如何使用cpp-netlib做異步http獲取請求

#include <iostream> 
#include <boost/network/protocol/http/client.hpp> 

using namespace std; 
using namespace boost::network; 
using namespace boost::network::http; 

typedef function<void(boost::iterator_range<char const *> const &, boost::system::error_code const &)> body_callback_function_type; // ERROR: Expected initializer before '<' token 

body_callback_function_type callback() // ERROR: 'body_callback_function_type' does not name a type 
{ 
    cout << "This is my callback" << endl; 
} 

int main() { 
    http::client client; 
    http::client::request request("http://www.google.com/"); 
    http::client::response response = client.get(request, http::_body_handler=callback()); // ERROR: 'callback' was not declared in this scope 
    cout << body(response) << endl; 
    return 0; 
} 

回答

3

我沒有用CPP-NETLIB,但它看起來像有一個與你的代碼一些明顯的問題:

第一個錯誤是在功能上的typedef缺少boost::

typedef function<void(boost::iterator_range<char const *> const &, boost::system::error_code const &)> body_callback_function_type; // ERROR: Expected initializer before '<' token 

應該

typedef boost::function<void(boost::iterator_range<char const *> const &, boost::system::error_code const &)> body_callback_function_type; 

第二個錯誤是:

body_callback_function_type callback() 
{ 
    cout << "This is my callback" << endl; 
} 

應該是正確的功能:

void callback(boost::iterator_range<char const *> const &, boost::system::error_code const &) 
{ 
    cout << "This is my callback" << endl; 
} 

第三個錯誤是,你應該通過回調,不叫它:

http::client::response response = client.get(request, http::_body_handler=callback()); 

應該

http::client::response response = client.get(request, callback); 

希望就是所有(或足夠讓你開始)。