본문 바로가기

C Programing

linux C언어 curl 사용하여 httpget요청보내기.

    • curl 설치.

최신버전 curl 다운로드 https://curl.haxx.se/download.html

[root@localhost ~]# wget https://curl.haxx.se/download/curl-7.49.1.tar.gz^C

[root@localhost ~]# tar zxvf curl-7.49.1.tar.gz

[root@localhost ~]# cd curl-7.49.1

[root@localhost curl-7.49.1]# ./configure

[root@localhost curl-7.49.1]# make

[root@localhost curl-7.49.1]# ./make install

curl.h 사용하여 GET 요청 보내고 응답내용 출력하기

 

#include <stdio.h>

#include <curl/curl.h>

#include <string.h>

#include <stdlib.h>



struct url_data {

    size_t size;

    char* data;

};



size_t write_data(void *ptr, size_t size, size_t nmemb, struct url_data *data) {

    size_t index = data->size;

    size_t n = (size * nmemb);

    char* tmp;



    data->size += (size * nmemb);



#ifdef DEBUG

    fprintf(stderr, "data at %p size=%ld nmemb=%ld\n", ptr, size, nmemb);

#endif

    tmp = realloc(data->data, data->size + 1); /* +1 for '\0' */



    if(tmp) {

        data->data = tmp;

    } else {

        if(data->data) {

            free(data->data);

        }

        fprintf(stderr, "Failed to allocate memory.\n");

        return 0;

    }



    memcpy((data->data + index), ptr, n);

    data->data[data->size] = '\0';



    return size * nmemb;

}



char *handle_url(char* url) {

    CURL *curl;



    struct url_data data;

    data.size = 0;

    data.data = malloc(4096); /* reasonable size initial buffer */

    if(NULL == data.data) {

        fprintf(stderr, "Failed to allocate memory.\n");

        return NULL;

    }



    data.data[0] = '\0';



    CURLcode res;



    curl = curl_easy_init();

    if (curl) {

        curl_easy_setopt(curl, CURLOPT_URL, url);

        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);

        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);

        res = curl_easy_perform(curl);

        if(res != CURLE_OK) {

                fprintf(stderr, "curl_easy_perform() failed: %s\n",

                        curl_easy_strerror(res));

        }

        curl_easy_cleanup(curl);

    }
    return data.data;
}

int main(int argc, char* argv[]) {
    char* data;

    if(argc < 2) {
        fprintf(stderr, "Must provide URL to fetch.\n");
        return 1;
    }
    data = handle_url(argv[1]);

    if(data) {
        printf("%s\n", data);
        free(data);
    }

    return 0;
}
  • 컴파일
[root@localhost lee]# gcc -lcurl httpget.c -o httpget

[root@localhost lee]# ls | grep http

httpget

httpget.c

 

 

  • 테스트
  • [root@localhost lee]# \./httpget leechul.tistory.com

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">



<head>



<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<link rel="alternate" type="application/rss+xml" title="LeeChul - Ti Story" href="http://leechul.tistory.com/rss" />

<link rel="stylesheet" media="screen" type="text/css" href="//s1.daumcdn.net/cfs.tistory/custom/blog/162/1625906/skin/style.css?_T_=1404878456" />

<link rel="shortcut icon" href="/favicon.ico" />

<title>LeeChul - Ti Story :: LeeChul - Ti Story</title>

<meta name="google-site-verification" content="klRr72mqA-5-t9W-G5plC3MtNLtNtRevAhkyty_IVoI" />

<meta name="robots" content="all"/>

<meta name="robots" content="index,follow">

<link rel="stylesheet" type="text/css" href="//s1.daumcdn.net/cfs.tistory/resource/4183/blog/plugins/A_ShareEntryWithSNS/css/shareEntryWithSNS.css"/>

<script type="text/javascript" src="//s1.daumcdn.net/cfs.tistory/resource/4183/blog/plugins/A_ShareEntryWithSNS/script/shareEntryWithSNS.js"></script>



<style type="text/css">

                #daumSearchBox {

                        height: 21px;

                        background-image : url(http://i1.daumcdn.net/imgsrc.search/search_all/show/tistory/plugin/bg_search2_2.gif);

                        margin: 5px auto ;

                        padding: 0;

                }

                #daumSearchBox input {

                        background: none;

                        margin : 0;

                        padding : 0; 

 

http get

 

 

  • curl 샘플코드

https://curl.haxx.se/libcurl/c/example.html

 

 

'C Programing' 카테고리의 다른 글

openssl lib 사용시 -lssl -lcrypto 로 링크 걸때 에러 발생시 해결방법.  (0) 2014.08.09
BITWISE OPERAT ORS  (0) 2013.12.31
if(0) and if(!0)  (0) 2013.12.17
string functions and operations  (0) 2013.12.08
multiple-source-files  (0) 2013.12.08