gpt4 book ai didi

c - 在 Linux 上使用 Easy Curl 通过 SSL 进行 FTP 上传

转载 作者:太空宇宙 更新时间:2023-11-04 08:47:30 24 4
gpt4 key购买 nike

我使用 libcurl 示例在 Linux 机器上开发了一个“C”应用程序 http://curl.askapache.com/c/ftpupload.html我工作很好。我被要求将 SSL 用于“控制 channel 和数据通道的数据加密”。我无法找到将 SSL 添加到我遵循的示例的示例。这是 FTP 程序的核心:

// get a FILE * of the same file
hd_src = fopen(local_file, "rb");

// curl init
curl_global_init(CURL_GLOBAL_ALL);

// get a curl handle
curl = curl_easy_init();

if (curl) { // build a list of commands to pass to libcurl
headerlist = curl_slist_append(headerlist, buf_1);
#ifdef DEBUG
// we want to use our own read function
curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
#endif

// enable uploading
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);

// specify target
curl_easy_setopt(curl, CURLOPT_URL, ftp_url);
curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
curl_easy_setopt(curl, CURLOPT_PORT, 21);

// pass in that last of FTP commands to run after the transfer
curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist);

// now specify which file to upload
curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);

// Set the size of the file to upload
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t) fsize);

// Now run off and do what you've been told!
res = curl_easy_perform(curl);

// Check for errors
if (res != CURLE_OK) {
char *s;
s = malloc((sizeof(char) * 100) + 1);
sprintf(s, "curl_easy_perform() failed: %s - Error Number: %d\n",
curl_easy_strerror(res), res);
returnResults->error = true;
returnResults->errorMessage = s;
return returnResults;
}

// clean up the FTP commands list
curl_slist_free_all(headerlist);

// always cleanup
curl_easy_cleanup(curl);
}

fclose(hd_src); // close the local file
curl_global_cleanup();

最佳答案

然而,在 libcurl 网站上有一个现有的示例代码,展示了如何使用 FTP-SSL 来下载一个文件:ftpsget.c - 它显示了您需要添加的非常少的 SSL 魔法。上传的魔力是一样的:

/* We activate SSL and we require it for both control and data */
curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);

事实上,您可以将这一行添加到 FTP upload example 中.它会为您进行 FTPS 上传。

关于c - 在 Linux 上使用 Easy Curl 通过 SSL 进行 FTP 上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21126358/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com