【问题标题】:Downloading a file using libcurl in C using SFTP: "Unsupported protocol" in c, but command line works使用 SFTP 在 C 中使用 libcurl 下载文件:c 中的“不支持的协议”,但命令行有效
【发布时间】:2020-08-11 10:19:58
【问题描述】:

我的代码基于Download file using libcurl in C/C++,但我收到一个错误:“不支持的协议” for SFTP。但是,对于相同的 SFTP,文件下载和上传使用命令行。

代码:

#include <stdio.h>
#include <curl/curl.h>

int main(void)
{
    CURL *curl;
    FILE *fp;
    CURLcode res;
    //char *url = "http://stackoverflow.com";
    char *url = "sftp://dheerajr@10.4.1.156/home/dheerajr/temp/download.txt";
    char outfilename[FILENAME_MAX] = "temp.txt";
    curl = curl_easy_init();                                                                                                                                                                                                                                                           
    if (curl)
    {   
        fp = fopen(outfilename,"wb");
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
        res = curl_easy_perform(curl);
        if(res == CURLE_OK)
            printf("Download Successfull\n");
        else
            printf("ERROR: %s\n", curl_easy_strerror(res));
        curl_easy_cleanup(curl);
        fclose(fp);
    }   
    return 0;
}

使用支持 SFTP 的 curl -V 命令检查。 curl -V 输出:

curl 7.72.0-DEV (x86_64-pc-linux-gnu) libcurl/7.72.0-DEV OpenSSL/1.1.1 zlib/1.2.11 libidn2/2.0.4 libpsl/0.19.1 (+libidn2/2.0.4) libssh2/1.8.2 nghttp2/1.30.0 librtmp/2.3
Release-Date: [unreleased]
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp scp sftp smb smbs smtp smtps telnet tftp 
Features: AsynchDNS HTTP2 HTTPS-proxy IDN IPv6 Largefile libz NTLM NTLM_WB PSL SSL TLS-SRP UnixSockets

谁能为此提出解决方案?

【问题讨论】:

  • 您有没有找到解决方案?我也有同样的问题!
  • 是的,我发现解决方案需要通过启用不同的选项进行编译

标签: c curl download sftp libcurl


【解决方案1】:

您需要先编译 ssh2 库,然后使用 --with-libssh2="path" 重新配置 curl 库并重新编译。然后您将使用 curl api 调用 ssh 函数。

祝你好运;-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-10
    • 2013-03-11
    • 1970-01-01
    • 1970-01-01
    • 2011-01-22
    • 2019-08-06
    • 2012-03-24
    • 2017-01-10
    相关资源
    最近更新 更多