我正在使用带有C ++的cURL-library从这个url中检索一个非常大的JSON字符串:https://www.bitstamp.net/api/order_book/?group=1(长度约为170 000个字符)
我的问题是cURL没有将整个字符串传递给我的回调函数。我用标准的回调函数测试了它,它将整个字符串打印到stdout,所以我可以肯定地说这不是接收数据的问题。但是一旦调用了我的回调函数,传递的数据的大小就是7793或15776个字符。 (我想不出任何可能与这两个数字的变化相关的变化)
我首先认为CURL_MAX_WRITE_SIZE值可能是原因因此我将其从16384更改为131072,但它也没有帮助:
#ifdef CURL_MAX_WRITE_SIZE
#undef CURL_MAX_WRITE_SIZE
#define CURL_MAX_WRITE_SIZE 131072
#endif
这是我的代码的简要概述:
class EXCHANGE
{
public:
enum { BASE_TYPE }; //to check if template-types are derived from this class
virtual size_t orderbookDataProcessing(void* buffer, unsigned int size, unsigned int nmemb) = 0;
template <typename TTYPE> //only for classes derived from EXCHANGE
static size_t orderbookDataCallback(void* buffer, unsigned int size, unsigned int nmemb, void* userdata)
{
//check if TTYPE is derived from EXCHANGE (BASE_TYPE is declared in EXCHANGE)
enum { IsItDerived = TTYPE::BASE_TYPE };
TTYPE* instance = (TTYPE*)userdata;
if(0 != instance->orderbookDataProcessing(buffer, size, nmemb))
{
return 1;
}
else
{
return (size*nmemb);
}
}
}
class BITSTAMP : public EXCHANGE
{
public:
size_t orderbookDataProcessing(void* buffer, unsigned int size, unsigned int nmemb)
{
string recvd_string; //contains the received string
//copy the received data into a new string
recvd_string.append((const char*)buffer, size*nmemb);
//do stuff with the recvd_string
return 0;
}
}
//main.cpp
///////////////////////////////////
CURL* orderbook_curl_handle = curl_easy_init();
curl_easy_setopt(orderbook_curl_handle, CURLOPT_URL, "https://www.bitstamp.net/api/order_book/?group=1") //URL to receive the get-request
curl_easy_setopt(orderbook_curl_handle, CURLOPT_HTTPGET, 1) //use get method
curl_easy_setopt(orderbook_curl_handle, CURLOPT_NOSIGNAL, 1) //required for multi-threading ?
curl_easy_setopt(orderbook_curl_handle, CURLOPT_WRITEDATA, &bitstamp_USD) //instance passed to the callback-function
curl_easy_setopt(orderbook_curl_handle, CURLOPT_WRITEFUNCTION, (size_t(*)(void*, unsigned int, unsigned int, void*))&EXCHANGE::orderbookDataCallback<BITSTAMP>) //callback function to recv the data
curl_easy_setopt(orderbook_curl_handle, CURLOPT_SSL_VERIFYPEER, FALSE) //do not verify ca-certificate
curl_easy_perform(orderbook_curl_handle);
答案 0 :(得分:3)
CURL在收到数据时调用您的写回调函数。如果您的数据太大(而您的数据太大),则会通过多次调用写回调给您。
您必须从每个写回调中收集数据,直到CURL告诉您响应完成。在您的情况下,这应该是curl_easy_perform
返回时。