cURL 是一個(gè)網(wǎng)絡(luò)數(shù)據(jù)傳輸項(xiàng)目,通常說(shuō) cURL 是指 curl 命令行工具,它支持 DICT、FILE、FTP、FTPS、Gopher、HTTP、HTTPS、IMAP、IMAPS、LDAP、LDAPS、POP3、POP3S、RTMP、RTSP、SCP、SFTP、SMB、SMBS、SMTP、SMTPS、Telnet 與 TFTP 等協(xié)議,而 curl 的底層使用的是 libcurl 庫(kù),libcurl 與 curl 組成了 cURL 項(xiàng)目。
curl 源碼下載 https://curl.se/download.html
下載之后三步:
1.解壓
2.進(jìn)入目錄:./configure --with-gnutls (打開(kāi)tls)
3.make
4.make install
測(cè)試命令:
curl -v https://www.baidu.com
curl -V
命令沒(méi)問(wèn)題的話我們?cè)创a編譯安裝成功,下面就可以寫(xiě)代碼實(shí)現(xiàn)了。
https實(shí)現(xiàn)下載功能:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-741609.html
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
static int download_progress;
struct progress {
char *private;
size_t size;
};
typedef struct HTTP_OPT
{
char url[128]; /*url of https*/
// char *user_key; // username:password
char *file; /*filepath*/
}HTTP_OPT;
static size_t callback_download_response(void *contents, size_t size, size_t nmemb, void *userp)
{
size_t realsize = size * nmemb;
int count = 0;
FILE* fp = (FILE *)userp;
if (!fp) {
/* out of memory! */
fprintf(stderr ,"not file to save download data .\n");
return 0;
}
if(contents && realsize > 0 ) {
count = fwrite(contents, 1, realsize, fp);
if (count != realsize) {
fprintf(stderr, "write file failed, size:%d != write_count:%d\n", realsize, count);
fflush(fp);
return 0;
}
}
fflush(fp);
return realsize;
}
static size_t progress_callback(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)
{
int progress = 0;
progress = (int)(dlnow / dltotal * 100);
fprintf(stderr, "download progress = %d / 100", progress);
download_progress = progress;
return 0;
}
int get_download_progress()
{
return download_progress;
}
CURL *curl_init() //curl初始化
{
download_progress = 0;
curl_global_init(CURL_GLOBAL_DEFAULT);
CURL *curl = curl_easy_init();
if(NULL == curl)
{
fprintf(stderr, "Init curl failed.\n");
exit(1);
}
return curl;
}
void curl_deinit(CURL *curl) //退出curl
{
curl_easy_cleanup(curl);
curl_global_cleanup();
}
int https_download(const HTTP_OPT http_option)
{
CURL *curl;
struct progress data;
FILE *fp = fopen(http_option.file, "w");
if(NULL == fp)
{
fprintf(stderr, "Open file failed at %s:%d\n", __FILE__, __LINE__);
return -1;
}
curl = curl_init();
if (curl) {
CURLcode res;
curl_easy_setopt(curl, CURLOPT_POST, 0);
curl_easy_setopt(curl, CURLOPT_URL, http_option.url);
//跳過(guò)證書(shū)校驗(yàn)
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
//6秒未達(dá)到30字節(jié)會(huì)退出
curl_easy_setopt(curl,CURLOPT_LOW_SPEED_TIME,6L);
curl_easy_setopt(curl,CURLOPT_LOW_SPEED_LIMIT,30L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback_download_response);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
//獲得進(jìn)度條
// curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, &data);
// curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_callback);
// curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
//res = curl_easy_getinfo(curl, CURLINFO_HTTP_CODE);
res = -1;
}
fclose(fp);
curl_deinit(curl);
return 0;
}
return -1;
}
int download_https(char* addr, char* refile, char* lofile)
{
HTTP_OPT http_opt;
sprintf(http_opt.url, "%s%s", addr, refile);
fprintf(stderr, "http_opt.url = %s\n",http_opt.url);
http_opt.file = lofile;
if (https_download(http_opt) != 0) {
fprintf(stderr, "Download failed.\n");
return -1;
} else {
fprintf(stderr, "Download success.\n");
return 0;
}
}
int main()
{
download_https("https://filesamples.com","/samples/image/bmp/sample_640×426.bmp", "sample_640×426.bmp" );
}
ftp實(shí)現(xiàn)下載功能文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-741609.html
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
typedef enum FTP_STATE
{
FTP_UPLOAD_SUCCESS,
FTP_UPLOAD_FAILED,
FTP_DOWNLOAD_SUCCESS,
FTP_DOWNLOAD_FAILED
}FTP_STATE;
typedef struct FTP_OPT
{
//char *url; /*url of ftp*/
char url[80]; /*url of ftp*/
char *user_key; /*username:password*/
char *file; /*filepath*/
}FTP_OPT;
char _ip_addr[16];//遠(yuǎn)程主機(jī)IP地址
static int get_file_size(FILE *file) //獲取文件大小
{
int size = 0;
fseek(file, 0L, SEEK_END);
size = ftell(file);
fseek(file, 0L, SEEK_SET);
return size;
}
static int getipaddr(const char* _host)
{
memset(_ip_addr, 0, 16);
/*通過(guò)域名得到相應(yīng)的ip地址*/
struct hostent *host = gethostbyname(_host);//此函數(shù)將會(huì)訪問(wèn)DNS服務(wù)器
if (!host) {
fprintf(stderr, "找不到IP地址,請(qǐng)檢查DNS服務(wù)器!");
return(-1);
}
for (int i = 0; host->h_addr_list[i]; i++) {
strcpy(_ip_addr, inet_ntoa( * (struct in_addr*) host->h_addr_list[i]));
break;
}
if (strlen(_ip_addr) == 0) {
fprintf(stderr, "錯(cuò)誤: 無(wú)法獲取到遠(yuǎn)程服務(wù)器的IP地址, 請(qǐng)檢查下載地址的有效性\n");
return(-1);
}
return 0;
}
CURL *curl_init() //curl初始化
{
curl_global_init(CURL_GLOBAL_DEFAULT);
CURL *curl = curl_easy_init();
if(NULL == curl)
{
fprintf(stderr, "Init curl failed.\n");
exit(1);
}
return curl;
}
void curl_set_upload_opt(CURL *curl, const char *url, const char *user_key, FILE *file) //設(shè)置上傳配置參數(shù)
{
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_USERPWD, user_key);
curl_easy_setopt(curl, CURLOPT_READDATA, file);
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
curl_easy_setopt(curl, CURLOPT_INFILESIZE, get_file_size(file));
curl_easy_setopt(curl, CURLOPT_FTP_CREATE_MISSING_DIRS, 1);
// curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
}
void curl_set_download_opt(CURL *curl, const char *url, const char *user_key, FILE *file) //設(shè)置下載配置參數(shù)
{
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_USERPWD, user_key);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
// curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
}
void curl_exit(CURL *curl) //退出curl
{
curl_easy_cleanup(curl);
curl_global_cleanup();
}
CURLcode curl_perform(CURL *curl) //執(zhí)行curl
{
CURLcode ret = curl_easy_perform(curl);
if(ret != 0)
{
fprintf(stderr, "Perform curl failed.\n");
curl_exit(curl);
exit(1);
}
return ret;
}
FTP_STATE ftp_upload(const FTP_OPT ftp_option) //ftp上傳文件
{
FTP_STATE state;
CURL *curl;;
FILE *fp = fopen(ftp_option.file, "r");
if(NULL == fp)
{
fprintf(stderr, "Open file failed at %s:%d\n", __FILE__, __LINE__);
return FTP_UPLOAD_FAILED;
}
curl = curl_init();
curl_set_upload_opt(curl, ftp_option.url, ftp_option.user_key, fp);
if(CURLE_OK == curl_perform(curl))
state = FTP_UPLOAD_SUCCESS;
else
state = FTP_UPLOAD_FAILED;
curl_exit(curl);
fclose(fp);
return state;
}
FTP_STATE ftp_download(const FTP_OPT ftp_option) // ftp下載文件
{
FTP_STATE state;
CURL *curl;
FILE *fp = fopen(ftp_option.file, "w");
if(NULL == fp)
{
fprintf(stderr, "Open file failed at %s:%d\n", __FILE__, __LINE__);
return FTP_UPLOAD_FAILED;
}
curl = curl_init();
curl_set_download_opt(curl, ftp_option.url, ftp_option.user_key, fp);
if(CURLE_OK == curl_perform(curl))
state = FTP_DOWNLOAD_SUCCESS;
else
state = FTP_DOWNLOAD_FAILED;
curl_exit(curl);
fclose(fp);
return state;
}
int download_ftp(char* addr, char* refile, char* lofile)
{
if(getipaddr(addr) < 0)
return -1;
FTP_OPT ftp_opt;
sprintf(ftp_opt.url, "ftp://%s%s", _ip_addr, refile);
fprintf(stderr, "ftp_opt.url = %s\n",ftp_opt.url);
ftp_opt.file = lofile; //指下載到本地文件名
ftp_opt.user_key = "anonymous:";
if(FTP_DOWNLOAD_SUCCESS == ftp_download(ftp_opt))
fprintf(stderr, "Download success.\n");
else
fprintf(stderr, "Download failed.\n");
return 0;
}
int main()
{
download_ftp("192.168.5.3","/samples/image/bmp/sample_640×426.bmp", "sample_640×426.bmp" );
}
到了這里,關(guān)于curl 實(shí)現(xiàn) https、ftp下載文件 代碼的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!