/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at http://curl.haxx.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
***************************************************************************/
/* Include libraries */
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
size_t func( void* ptr, size_t size, size_t nmemb, void* stream);
int main(void)
{
CURL *curl;
CURLcode res;
char* header;
char* body;
header = (char*)calloc(100000, sizeof(char));
body = (char*)calloc(100000, sizeof(char));
curl = curl_easy_init(); // Initialization 코드
if(curl) {
// ID PWD 항목에 해당 ID와 PWD를 넣으면 알아서, ID PWD를 넣어준다.
// curl_easy_setopt(curl, CURLOPT_USERPWD, " ID : PWD");
// URL 을 사용하는 코드 example.com에 해당 URL 을 넣으면 URL 을 저장한다.
curl_easy_setopt(curl, CURLOPT_URL, www.example.com);
// FOLLOWLOCATION을 1로 보내면, Redirection 을 모두 따라 간다.
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); // Allow redirection
// 아래 두 줄은 Post 메세지에 메세지를 실어 보내기 위한 방법이다.
// curl_easy_setopt(curl, CURLOPT_POST, 1);
// curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "ID : PWD");
//WriteFunction 에 Function 등록하면 Callback으로 메세지를 받게 된다.
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, func);
//WriteHeader 옵션으로 헤더 받음
curl_easy_setopt(curl, CURLOPT_WRITEHEADER, header);
//WriteData 옵션으로 데이터 받음. WriteFunction의 Callback 메세지를 Header와 Body로
//나누어 받은 것
curl_easy_setopt(curl, CURLOPT_WRITEDATA, body);
// 위에 입력한 옵션들을 실행하는 함수
res = curl_easy_perform(curl);
/* always cleanup */
//함수 실행 후 종료 (메모리삭제)
curl_easy_cleanup(curl);
}
printf("\n\nHEADER is\n%s\n\n\n", header);
printf("\n\nBODY is\n%s\n\n\n", body);
free(header);
free(body);
return 0;
}
size_t func( void* ptr, size_t size, size_t nmemb, void* stream)
{
strncat( (char*)stream, (char*)ptr, size*nmemb);
return size*nmemb;
}