人氣點閱:2469 |
發表人 |
libcurl: url2file.c & url2buffer.c (GET) 2013-10-01 10:25 分類: ✔️include ✔️include ✔️include ✔️include ✔️include ✔️include 個人: ✔️include ✔️include ✔️include ✔️include ✔️include ✔️include |
/ / /
|
|
libcurl: html2file.c & html2buffer.c (GET)
More Ref: http://curl.haxx.se/
url2buffer C Code Example
代碼:
#include <string.h>
#include <curl/curl.h>
struct string
{
char *ptr;
size_t len;
};
void init_string(struct string *s)
{
s->len = 0;
s->ptr = malloc(s->len+1);
if (s->ptr == NULL)
{
fprintf(stderr, "malloc() failed\n");
exit(EXIT_FAILURE);
}
s->ptr[0] = '\0';
return;
}
size_t curl_writefunc(void *ptr, size_t size, size_t nmemb, struct string *s)
{
size_t new_len = s->len + size*nmemb;
s->ptr = realloc(s->ptr, new_len+1);
if (s->ptr == NULL)
{
fprintf(stderr, "realloc() failed\n");
exit(EXIT_FAILURE);
}
memcpy(s->ptr+s->len, ptr, size*nmemb);
s->ptr[new_len] = '\0';
s->len = new_len;
return size*nmemb;
}
int main(void)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl)
{
struct string s;
init_string(&s);
curl_easy_setopt(curl, CURLOPT_URL, "http://www.google.com.tw/");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
printf("%s\n", s.ptr);
free(s.ptr);
curl_easy_cleanup(curl);
}
return 0;
}
url2file C Code Example
代碼:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <curl/curl.h>
static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
return written;
}
int main(int argc, char *argv[])
{
CURL *curl_handle;
static const char *pagefilename = "page.out";
FILE *pagefile;
curl_global_init(CURL_GLOBAL_ALL);
/* init the curl session */
curl_handle = curl_easy_init();
/* set URL to get here */
curl_easy_setopt(curl_handle, CURLOPT_URL, buf_url);
/* disable progress meter, set to 0L to enable and disable debug output */
curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);
/* send all data to this function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);
/* open the file */
pagefile = fopen(pagefilename, "wb");
if (pagefile)
{
printf("filep=%p\n", (void *)pagefile);
/* write the page body to this file handle. CURLOPT_FILE is also known as
CURLOPT_WRITEDATA*/
curl_easy_setopt(curl_handle, CURLOPT_FILE, pagefile);
/* get it! */
curl_easy_perform(curl_handle);
/* close the header file */
fclose(pagefile);
}
/* cleanup curl stuff */
curl_easy_cleanup(curl_handle);
return 0;
}
代碼:
$ make url2file.c -l curl
$ make url2buffer.c -l curl
________________
美術插畫設計案子報價系統 v0.1 Beta
爪哇禾雀
|
|
Type
繪圖畫廊 設計藝廊 攝影相簿 留言板 最愛收藏 分類標籤
暱稱: Type 註冊: 2002-11-30 發表: 11166 來自: vovo2000.com
V幣: 901741
|
|
|
|
|