国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

curl 實(shí)現(xiàn) https、ftp下載文件 代碼

這篇具有很好參考價(jià)值的文章主要介紹了curl 實(shí)現(xiàn) https、ftp下載文件 代碼。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

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)下載功能:

#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)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • C# 使用FTP上傳文件、下載文件,實(shí)現(xiàn)數(shù)據(jù)傳輸

    上傳文件的方法調(diào)用: 下載文件方法:

    2024年02月14日
    瀏覽(25)
  • Java基于ftp協(xié)議實(shí)現(xiàn)文件的上傳和下載

    Java基于ftp協(xié)議實(shí)現(xiàn)文件的上傳和下載

    相比其他協(xié)議,如 HTTP 協(xié)議,F(xiàn)TP 協(xié)議要復(fù)雜一些。與一般的 C/S 應(yīng)用不同點(diǎn)在于一般的C/S 應(yīng)用程序一般只會(huì)建立一個(gè) Socket 連接,這個(gè)連接同時(shí)處理服務(wù)器端和客戶端的連接命令和數(shù)據(jù)傳輸。而FTP協(xié)議中將命令與數(shù)據(jù)分開(kāi)傳送的方法提高了效率。 FTP 使用 2 個(gè)端口,一個(gè)數(shù)據(jù)

    2024年02月11日
    瀏覽(29)
  • linux編譯curl庫(kù)(支持https)

    linux編譯curl庫(kù)(支持https)

    openssl下載和編譯 https://www.openssl.org/source/old/ 解壓 配置 如果是編譯靜態(tài)庫(kù)加入 -fPIC no-shared 如果指定安裝路徑,使用 --prefix=/usr/local/openssl/ 選項(xiàng)指定特定目錄 編譯和安裝 curl下載和編譯 https://curl.se/download.html 解壓 配置 如果想要編譯靜態(tài)庫(kù),加入 --disable-shared 選項(xiàng) 如果需要指

    2024年02月09日
    瀏覽(18)
  • 【Linux】基于FTP協(xié)議實(shí)現(xiàn)Linux與Windows文件傳輸

    【Linux】基于FTP協(xié)議實(shí)現(xiàn)Linux與Windows文件傳輸

    基于FTP協(xié)議實(shí)現(xiàn)Linux與Winodows實(shí)現(xiàn)文件傳輸,是大學(xué)期間的一個(gè)小實(shí)驗(yàn)。在這里做個(gè)總結(jié)。 實(shí)驗(yàn)環(huán)境: Linux CentOS 7.9 Xshell 7 Win10 通過(guò)yum安裝vxftpd pacakge,并按照如下指令執(zhí)行 修改 vsftpd.conf,此前先備份 成 vsftpd.conf.bak ,防止該配置文件改錯(cuò)導(dǎo)致無(wú)法運(yùn)行。 執(zhí)行 vim vsftpd.conf ,加

    2024年02月04日
    瀏覽(32)
  • Linux系統(tǒng)編程,使用C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的FTP(服務(wù)器/客戶端)

    Linux系統(tǒng)編程,使用C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的FTP(服務(wù)器/客戶端)

    前言 跟著上官社長(zhǎng) 陳哥花了一個(gè)月的時(shí)間終于把Linux系統(tǒng)編程學(xué)的差不多了,這一個(gè)月真的是頭疼啊,各種bug,調(diào)的真心心累,不過(guò)好在問(wèn)題都解決掉了,在此也感謝一下答疑老師,給我提供了很多的思路,本文章是對(duì)前段時(shí)間學(xué)習(xí)Linux,做一個(gè)小小的總結(jié),才疏學(xué)淺,只學(xué)

    2024年02月12日
    瀏覽(23)
  • Curl【實(shí)例 01】curl下載使用及cmd實(shí)例腳本分享(通過(guò)請(qǐng)求下載文件)

    Curl【實(shí)例 01】curl下載使用及cmd實(shí)例腳本分享(通過(guò)請(qǐng)求下載文件)

    Curl 官方下載地址 可下載不同平臺(tái)不同版本的安裝包,本次使用的是Windows解壓版本 curl-8.0.1_9-win32-mingw.zip 。 1.1 curl curl是一個(gè)開(kāi)源的命令行工具和庫(kù),用于在終端和腳本中進(jìn)行網(wǎng)絡(luò)數(shù)據(jù)傳輸。它支持多種協(xié)議,如HTTP、HTTPS、FTP、SMTP等,可以通過(guò)URL進(jìn)行數(shù)據(jù)傳輸和通信。 curl的

    2024年02月07日
    瀏覽(44)
  • 如何使用curl下載github代碼

    如何使用curl下載github代碼

    首先通過(guò)chrome打開(kāi)想要下載的源文件 如圖,有那個(gè)下載圖標(biāo)時(shí)表示不需要鑒權(quán)即可下載,一般倉(cāng)庫(kù)都會(huì)開(kāi)放只讀權(quán)限,所以很大概率都有 比如我想下載這個(gè)crc32.c文件 那么我就需要知道它在哪個(gè)IP中,按下F12打開(kāi)網(wǎng)絡(luò),點(diǎn)擊下載圖標(biāo) 上圖為文件所在位置 使用如下命令進(jìn)行下

    2024年02月16日
    瀏覽(25)
  • Java實(shí)現(xiàn)minio上傳、下載、刪除文件,支持https訪問(wèn)

    Java實(shí)現(xiàn)minio上傳、下載、刪除文件,支持https訪問(wèn)

    MinIO 是一款高性能、分布式的對(duì)象存儲(chǔ)系統(tǒng),Minio是基于Go語(yǔ)言編寫(xiě)的對(duì)象存儲(chǔ)服務(wù),適合于存儲(chǔ)大容量非結(jié)構(gòu)化的數(shù)據(jù),例如圖片、音頻、視頻、備份數(shù)據(jù)等 , 傳統(tǒng)對(duì)象存儲(chǔ)用例(例如輔助存儲(chǔ),災(zāi)難恢復(fù)和歸檔)方面表現(xiàn)出色。 導(dǎo)入minio依賴(lài)包 application.yml配置文件 配置

    2024年02月05日
    瀏覽(27)
  • 命令行下載FTP文件

    命令行下載FTP文件

    目錄 ?介紹本次用到的 DOS 命令 1. 打開(kāi)命令行 2. 進(jìn)入 FTP 3. 連接 FTP 4. 輸入用戶名及密碼 5. 查看 FTP 文件目錄 6. 進(jìn)入【HIS】文件夾 7. 指定本地文件夾 8. 開(kāi)關(guān)交互模式 9. 下載文件 10. 下載時(shí)會(huì)有提示 11. 耗時(shí)計(jì)算 ????????本文旨在說(shuō)明如何以命令行的方式直接下載 FTP 上的

    2023年04月17日
    瀏覽(17)
  • 基于curl 使用http多線程下載大文件

    如需完整代碼,可評(píng)論區(qū)留言

    2024年02月04日
    瀏覽(27)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包