'programming/C_C++'에 해당되는 글 279건

  1. 2013.08.27 curl test

curl test

programming/C_C++ 2013. 8. 27. 13:49
반응형


// http://pss821213.blog.me/40114552784

화면출력대신 메모리 변수에 출력하여 개발할 때 사용할 수 있도록 완성한 소스를 올려본다

 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
 
struct MemoryStruct
{
    char    *memory;
    size_t  size;
};
 
static size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
{
    size_t realsize = size * nmemb;
    struct MemoryStruct *mem = (struct MemoryStruct *)data;
 
    mem->memory = realloc(mem->memory, mem->size + realsize + 1);
    if (mem->memory == NULL)
    {
        /* out of memory! */
        printf("not enough memory (realloc returned NULL)\n");
        exit(-1);
    }
 
    memcpy(&(mem->memory[mem->size]), ptr, realsize);
    mem->size += realsize;
    mem->memory[mem->size] = 0;
 
    return realsize;
}
 
int main(void)
{
    CURL *curl;
    CURLcode res;
    struct MemoryStruct chunk;
 
    chunk.memory = malloc(1);   /* will be grown as needed by the realloc above */
    chunk.size = 0;                         /* no data at this point */
    curl_global_init(CURL_GLOBAL_ALL);
 
    /* init the curl session */
    curl = curl_easy_init();
    if(curl)
    {

        /* specify URL to get */
        curl_easy_setopt(curl, CURLOPT_URL, https://www.cia.gov/);
       
#ifdef SKIP_PEER_VERIFICATION
        /*
         * If you want to connect to a site who isn't using a certificate that is
         * signed by one of the certs in the CA bundle you have, you can skip the
         * verification of the server's certificate. This makes the connection
         * A LOT LESS SECURE.
         *
         * If you have a CA cert for the server stored someplace else than in the
         * default bundle, then the CURLOPT_CAPATH option might come handy for
         * you.
         */
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
#endif
 
#ifdef SKIP_HOSTNAME_VERFICATION
        /*
         * If the site you're connecting to uses a different host name that what
         * they have mentioned in their server certificate's commonName (or
         * subjectAltName) fields, libcurl will refuse to connect. You can skip
         * this check, but this will make the connection less secure.
         */
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
#endif
 
        /* send all data to this function */
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
 
        /* we pass our 'chunk' struct to the callback function */
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
 
        /* get it! */
        res = curl_easy_perform(curl);
       
printf("%d\n", res);
 
        /* always cleanup */
        curl_easy_cleanup(curl);
 
        /* 여기서 처리 */
        /* chunk.memory 포인터를 접근하면 응답받은 데이터를 사용할 수 있다. */
        /* 주의점: 동적메모리할당을 해서 사용중이므로 다 썼으면 free 해 주어야 한다. */
       
        printf("%lu bytes retrieved\n", chunk.size);
        printf("%s\n", chunk.memory);
 
        if (chunk.memory)
        {
            free(chunk.memory);
        }
    }
 
    /* we're done with libcurl, so clean it up */
    curl_global_cleanup();
   
    return 0;
   
}

반응형
Posted by 공간사랑
,