Curl reads the page and displays the source.

Asked 1 years ago, Updated 1 years ago, 36 views

Let me ask you a question. Thank you.
I want to read and output web pages using CURL in C language, but it doesn't work well.

Here's the code.

#include "stdio.h"
# include "stdlib.h"
# include "string.h"
# include "curl/curl.h"

US>structure Buffer {
    char*data;
    int data_size;
};
size_t buffer_writer(char*ptr, size_t size, size_tnmemb, void*stream){
    structure Buffer*buf=(struct Buffer*)stream;
    char block = size * nmemb;  
    if(!buf)return block;
    if(!buf->data){
        buf->data=(char*)malloc(block);
    } else {
        buf->data=(char*)realloc(buf->data,buf->data_size+size);
    }
    if(buf->data){
        memcpy(buf->data,ptr,block);
        buf->data_size+=block;
    }
    printf("end");
    return block;
}
int main(intargc, const char*argv[]){   
    structure Buffer*buf;
    buf=(struct buffer*) malloc(sizeof(struct buffer));
    buf->data=NULL;
    buf->data_size=0;
    CURL*curl=curl_easy_init();
    curl_easy_setopt(curl, CURLOPT_URL, "https://www.yahoo.co.jp/");
    curl_easy_setopt(curl,CURLOPT_SSL_VERIFYPEER, 0);
    curl_easy_setopt(curl,CURLOPT_WRITEDATA,buf);
    curl_easy_setopt(curl,CURLOPT_HEADER,buffer_writer);
    curl_easy_cleanup(curl);
    printf("%d", buf->data_size);
    free(buf->data);
    free(buf);
    return EXIT_SUCCESS;
}

Please let me know if there is anything strange.
The result of this state is '0'.

c

2022-09-30 21:24

1 Answers

curl_easy_setopt() is the setup of the process and has not done anything yet.
Call curl_easy_perform(curl) for actual execution.


2022-09-30 21:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.