curl-library
How can I download multiple files using http protocol
Date: Thu, 25 Apr 2013 10:35:18 +0800
I want to write a program,downloading multiple files using http protocol
with libcurl.so I wrote like this.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <curl/curl.h>
const char *urls[3] = {
"http://mirrors.163.com/ubuntu-releases/robots.txt",
"http://mirrors.163.com/ubuntu-releases/favicon.ico",
"http://mirrors.163.com/ubuntu-releases/HEADER.html"
};
#define SIZE sizeof(urls)/sizeof(char*)
char * find_the_last_symbol(const char* string,int n)
{
char *strings = (char*)malloc(n);#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <curl/curl.h>
const char *urls[3] = {"http://mirrors.163.com/ubuntu-releases/robots.txt",
"http://mirrors.163.com/ubuntu-releases/favicon.ico",
"http://mirrors.163.com/ubuntu-releases/HEADER.html"};
#define SIZE sizeof(urls)/sizeof(char*)
char * find_the_last_symbol(const char* string,int n)
{
char *strings = (char*)malloc(n);
strncpy(strings,string,n);
while(strings[n--] != '/');
++n;
char *ptr;
ptr = &strings[++n];
free(strings);
return ptr;
}
size_t write(char *ptr,size_t size,size_t nmemb,void *userdata)
{
return fwrite(ptr,size,nmemb,(FILE*)userdata);
}
void init(CURL *curl,const char* url,int n)
{
char *name = find_the_last_symbol(url,n);
printf("this is in init function open file and write data in
%s\n",name);
FILE *filepointer = fopen(name,"w");
FILE *errorfile = fopen("errorfile","w");
curl_easy_reset(curl);
curl_easy_setopt(curl,CURLOPT_URL,url);
curl_easy_setopt(curl,CURLOPT_VERBOSE,1L);
curl_easy_setopt(curl,CURLOPT_STDERR,errorfile);
curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,write);
curl_easy_setopt(curl,CURLOPT_WRITEDATA,filepointer);
curl_easy_perform(curl);
fclose(filepointer);
fclose(errorfile);
}
int main(int argc,char **argv)
{
CURL *curl;
CURLcode res;
const char **curls = urls;
FILE *errorfile = fopen("errorfile","w");
curl = curl_easy_init();
int i = 0;
for(;i<SIZE;++i)
{
init(curl,urls[i],strlen(urls[i]));
}
curl_easy_cleanup(curl);
return 0;
}
strncpy(strings,string,n);
while(strings[n--] != '/');
++n;
char *ptr;
ptr = &strings[++n];
free(strings);
return ptr;
}
size_t write(char *ptr,size_t size,size_t nmemb,void *userdata)
{
return fwrite(ptr,size,nmemb,(FILE*)userdata);
}
void init(CURL *curl,const char* url,int n)
{
char *name = find_the_last_symbol(url,n);
printf("this is in init function open file and write data in
%s\n",name);
FILE *filepointer = fopen(name,"w");
FILE *errorfile = fopen("errorfile","w");
curl_easy_reset(curl);
curl_easy_setopt(curl,CURLOPT_URL,url);
curl_easy_setopt(curl,CURLOPT_VERBOSE,1L);
curl_easy_setopt(curl,CURLOPT_STDERR,errorfile);
curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,write);
curl_easy_setopt(curl,CURLOPT_WRITEDATA,filepointer);
curl_easy_perform(curl);
fclose(filepointer);
fclose(errorfile);
}
int main(int argc,char **argv)
{
CURL *curl;
CURLcode res;
const char **curls = urls;
FILE *errorfile = fopen("errorfile","w");
curl = curl_easy_init();
int i = 0;
for(;i<SIZE;++i)
{
init(curl,urls[i],strlen(urls[i]));
}
curl_easy_cleanup(curl);
return 0;
}
as the program you can see ,the files` url I have identified,now my
problem is that if I do not know the files` url,or I just know the web`
url ,how can download the files in this website,for example the website
mirrors.163.com,I know using ftp protocol can download multiple
files.Can anyone help me deal with this problem.
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2013-04-25