背景:
我正在使用该库编写我的第一个 C 程序,我需要收集发送到 SMTP 服务器的每个命令的响应。
我已经使用 curl_easy_setopt(curl_handle, CURLOPT_HEADERFUNCTION, parse_head)
向 SMTP 服务器发送命令并打印响应 header ,但我使用的是多线程选项。当我收到响应时,根本不清楚它是由哪个命令引起的。我假设它们不一定会按照发送的顺序收到。这是正确的吗?
这使得事情变得更加困难,因为库在没有我的显式请求的情况下处理一些调用(例如设置初始连接),所以我需要处理比显式请求更多的 header 。这将是可预测和可重复的,但肯定会增加额外的复杂性。
问题:
是否有一种“好的”方法可以使用多线程来准确确定哪个命令导致哪个响应 header ?
此外,适度相关的是,该库是否支持返回数字返回代码,或者我是否必须手动解析它?查了一下图书馆,好像没有。我只是想确定一下。
最佳答案
I am assuming that they will not necessarily be received in the same order sent. Is that correct?
是的,确实如此。这就是多线程的工作原理。
Is there a "good" way to determine exactly which command resulted in which response header using multi thread?
是的。您可以使用 CURLOPT_HEADERDATA
选项设置用户数据(上下文信息,无论您如何调用它) - 这将作为 header 函数的第四个参数传入。所以你可以编写这样的代码:
CURL *hndl = curl_easy_init();
// ...
curl_easy_setopt(hndl, CURLOPT_HEADERFUNCTION, parse_head);
curl_easy_setopt(hndl, CURLOPT_HEADERDATA, some_pointer_that_identifies_the_thread);
// ...
size_t parse_head(void *buf, void *size_t sz, size_t nmemb, void *context)
{
// context will be the pointer identifying the thread
}
does the library support returning the numeric return code or do I have to manually parse that out?
是的,确实如此:
long httpStatus;
curl_easy_getinfo(hndl, CURLINFO_RESPONSE_CODE, &httpStatus);
if (200 <= httpStatus && httpStatus < 300) {
// HTTP 2XX OK
} else {
// Error (4XX, 5XX) or redirect (3XX)
}
关于c - libcurl 库是否提供了任何方法来确定哪个响应头来自哪个命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13544692/