Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过CURLOPT_HEADERFUNCTION提取标题信息? < >我想用C++程序中的CURLoptTHead函数提取标题信息。_C++_Visual Studio_Libcurl - Fatal编程技术网

如何通过CURLOPT_HEADERFUNCTION提取标题信息? < >我想用C++程序中的CURLoptTHead函数提取标题信息。

如何通过CURLOPT_HEADERFUNCTION提取标题信息? < >我想用C++程序中的CURLoptTHead函数提取标题信息。,c++,visual-studio,libcurl,C++,Visual Studio,Libcurl,提供了关于如何获取这些头信息的解决方案,但我想知道为什么我的代码不起作用,以及一个可能的解决方案和示例 //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 funct

提供了关于如何获取这些头信息的解决方案,但我想知道为什么我的代码不起作用,以及一个可能的解决方案和示例

//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函数,返回特定的头信息
大小读取头(字符*头,大小大小,大小单位,无效*用户数据){
Erza oprations;//包含startsWith等字符串函数的类
if(oprations.startsWith(标题,“内容长度:”){
std::string header_in_string=oprations.replaceAll(header,“Content Length:”,“”);
long size=atol(header_in_string.c_str());
file\u size=size;//file\u size是全局变量

std::cout如github仓库所示,
操作(操作!)是一个局部变量,将在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 );
}
class-Erza:公共事业{
//…你的班子
公众:
bool下载(常量字符*url,常量字符*路径){
curl=curl_easy_init();
if(curl){
fp=fopen(路径,“wb”);
curl_easy_setopt(curl,CURLOPT_URL,URL);
curl_easy_setopt(curl,CURLOPT_CAINFO,“./ca bundle.crt”);
curl\u easy\u setopt(curl,CURLOPT\u SSL\u VERIFYPEER,false);
curl\u easy\u setopt(curl,CURLOPT\u SSL\u VERIFYHOST,false);
curl\u easy\u setopt(curl,CURLOPT\u header函数,readHeader);

curl_easy_setopt(curl,CURLOPT_HEADERDATA,this);//在显示的代码末尾,它应该是
std::cout它的文件大小,我已经编辑过了。很难说问题出在哪里,但是如果
readHeader()
捕获正确的值,最后,值被更改,程序中可能存在错误,
文件大小
内存被覆盖。AnalysisHeader的返回类型将是size\t,其他一切都很完美