简体   繁体   English

libcurl:curl_easy_perform块,除非设置了CURLOPT_READFUNCTION

[英]libcurl: curl_easy_perform blocks unless CURLOPT_READFUNCTION is set

I am trying to use libcurl C++ to make REST/HTTP requests. 我正在尝试使用libcurl C ++进行REST / HTTP请求。 I noticed curl_easy_perform blocks but if I set CURLOPT_READFUNCTION it doesn't. 我注意到curl_easy_perform块,但是如果我设置CURLOPT_READFUNCTION则不会。 I just want to understand why that is, I am new to libcurl or HTTP/REST protocol. 我只是想了解为什么,我是libcurl或HTTP / REST协议的新手。

Here is the code: 这是代码:

m_pCurl = curl_easy_init();
curl_easy_setopt(m_pCurl, CURLOPT_URL, "https://blahblahblah/api/auth/user/login");      
curl_easy_setopt(m_pCurl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(m_pCurl, CURLOPT_POST, 1);
curl_easy_setopt(m_pCurl, CURLOPT_COOKIE, "SKEY=BLAHBLAHBLAH");

struct curl_slist *list = NULL;

list = curl_slist_append(list, "Accept: application/json");
list = curl_slist_append(list, "Connection: keep-alive");
list = curl_slist_append(list, "Expect:");
list = curl_slist_append(list, "Content-Type: application/json");
list = curl_slist_append(list, "x-website-parameters: LALALALA");
curl_easy_setopt(m_pCurl, CURLOPT_HTTPHEADER, list);

// Callbacks
readarg_t rarg;

// readcb is a callback function
// Removing the two lines below will cause curl_easy_perform to hang
curl_easy_setopt(m_pCurl, CURLOPT_READFUNCTION, readcb);
curl_easy_setopt(m_pCurl, CURLOPT_READDATA, &rarg);

CURLcode res = curl_easy_perform(m_pCurl);

Note: Some of the encoded data are changed above. 注意:某些编码数据已在上面更改。

Any help would be greatly appreciated. 任何帮助将不胜感激。

Thanks, K 谢谢,K

According to The Manual ... 根据手册 ...

CURLOPT_READFUNCTION explained CURLOPT_READFUNCTION解释

... ...

If you set this callback pointer to NULL, or don't set it at all, the default internal read function will be used. 如果将此回调指针设置为NULL,或者根本不设置,则将使用默认的内部读取功能。 It is doing an fread() on the FILE * userdata set with CURLOPT_READDATA. 它正在使用CURLOPT_READDATA设置的FILE *用户数据上执行fread()。

However you also don't set CURLOPT_READDATA . 但是,您也没有设置CURLOPT_READDATA So looking again at The manual ... 因此,再次查看《手册》 ...

CURLOPT_READDATA explained CURLOPT_READDATA说明

... ...

By default, this is a FILE * to stdin. 默认情况下,这是stdin的FILE *。

So the reason your program "hangs" appears to be because it is waiting for something to arrive on the standard input stdin . 因此,您的程序“挂起”的原因似乎是因为它正在等待标准输入 stdin上的内容到达。

So the way it is supposed to work is this. 因此,它的工作方式应该是这样。

1) If you do nothing the data sent to the server comes from the standard input (which is often the keyboard). 1)如果您什么也不做,则发送到服务器的数据来自标准输入 (通常是键盘)。

2) If you set only CURLOPT_READDATA then it must be a FILE* you opened to an input file that contains the data you want to send. 2)如果 CURLOPT_READDATA则它必须是您打开到包含要发送的数据的输入文件的FILE*

3) If you set CURLOPT_READFUNCTION then CURLOPT_READDATA can point to anything your function needs to fulfil its task of sending data to the server. 3)如果设置了CURLOPT_READFUNCTIONCURLOPT_READDATA可以指向您的函数完成其向服务器发送数据的任务所需的任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM