gpt4 book ai didi

c++ - 如何通过CURLOPT_HEADERFUNCTION提取头部信息?

转载 作者:行者123 更新时间:2023-11-30 04:52:24 28 4
gpt4 key购买 nike

我想在我的 C++ 程序中使用 CURLOPT_HEADERFUNCTION 提取标题信息。

How can I use CURLOPT_HEADERFUNCTION to read a single response header field?提供了有关如何获取这些 header 信息的解决方案,但我想知道为什么我的代码不起作用以及可能的示例解决方案。

//readHeader function which returns the specific header information

size_t readHeader(char* header, size_t size, size_t nitems, void *userdata) {
Erza oprations; //class which contains string function like startsWith etc
if (oprations.startsWith(header, "Content-Length:")) {
std::string header_in_string = oprations.replaceAll(header, "Content-Length:", "");
long size = atol(header_in_string.c_str());
file_size = size; // file_size is global variable
std::cout << size; // here it is showing correct file size
}
else if (oprations.startsWith(header, "Content-Type:")) {
// do something
}else
// do something
return size * nitems;
}
// part of main function
curl = curl_easy_init();
if (curl) {
fp = fopen(path, "wb");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_CAINFO, "./ca-bundle.crt");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, readHeader);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);

fclose(fp);
std::cout << file_size; // showing value 0

在 readHeader 函数中获取正确的文件大小,但在 main 函数中获取 0 字节。

最佳答案

如您的 github depot 所示,oprations(操作!?)是一个局部变量,将在 readHeader 函数结束时释放。处理 readHeader 函数并为给定 Erza 实例获取正确文件大小的一种方法是将其指针传递给 userdata 值。 Erza 类可以重写为:

class Erza : public Endeavour {

//... your class body

public:
bool download (const char *url,const char* path){
curl = curl_easy_init();
if (curl) {
fp = fopen(path, "wb");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_CAINFO, "./ca-bundle.crt");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);

curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, readHeader);
curl_easy_setopt(curl, CURLOPT_HEADERDATA, this ); //<-- set this pointer to userdata value used in the callback.

curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);

curl_easy_cleanup(curl);
fclose(fp);
return false;
}else
return true;

}

size_t analyseHeader( char* header, size_t size, size_t nitems ){

if (startsWith(header, "Content-Length:")) {
std::string header_in_string = replaceAll(header, "Content-Length:", "");
long size = atol(header_in_string.c_str());
file_size = size; // file_size is a member variable
std::cout << size; // here it is showing correct file size
}
else if (startsWith(header, "Content-Type:")) {
// do something
}else
// do something
return size * nitems;
}

}//Eof class Erza

size_t readHeader(char* header, size_t size, size_t nitems, void *userdata) {

//get the called context (Erza instance pointer set in userdata)

Erza * oprations = (Erza *)userdata;
return oprations->analyseHeader( header, size, nitems );
}

关于c++ - 如何通过CURLOPT_HEADERFUNCTION提取头部信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54416179/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com