参数列表太长 libcurl c 程序错误

Argument list too long libcurl c program error

我正在使用 C 程序连接到 ftp 服务器并上传文件。以前服务器没有使用 SSL,我可以使用以下代码上传文件:

curl_global_init(CURL_GLOBAL_ALL);

curl = curl_easy_init();

if(curl)
{

    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, throw_away);

    curl_easy_setopt(curl, CURLOPT_USERNAME, "username");
    curl_easy_setopt(curl, CURLOPT_PASSWORD, "password");

    if (CURLE_OK != (res = curl_easy_setopt(curl, CURLOPT_URL, "ftp://ftpb.****.com")))
    {
        printf("Failed to check ftp url, Error : %s : %d\n", curl_easy_strerror(res), res);
    }
    curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);

    // Connection establishment timeout
    curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 20);
    printf("Trying to connect...\n");
    if (CURLE_OK != (res = curl_easy_perform(curl)))
    {
        /* If fail to connect */
        printf("FTP connection failed...%s : %d\n", strerror(res), res);
    }
    else
    {
        /* If connected succesfully */
        printf("Connected to FTP successfully...\n");

      /****** proceed with upload ******/
    }
}

在 ftp 服务器切换为使用 SSL 认证后,我在代码中添加了以下行以使用 SSL:

    curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);

现在,当我 运行 程序时,我的应用程序无法连接到 FTP 并显示消息:

"FTP connection failed...Argument list too long : 7"

即使我不添加 CURLOPT_USE_SSL 部分,也会出现相同的错误。

我可以使用 Filezilla 连接到 ftp 服务器(仅当我向地址添加 ftps 时)。在我的代码中添加 ftps 会出现错误:

"FTP connection failed...Device not a stream : 60"

有人知道解决这个问题的方法吗?

您不能在来自 libcurl 的 return 代码上使用 strerror()。 libcurl 为其 return 代码提供了自己的 strerror() 版本。参见 curl_easy_strerror

您还可以在 libcurl-errors 手册页上查看错误,在那里您了解到 7 表示 CURLE_COULDNT_CONNECT: "Failed to connect() to host or proxy"。这可能是因为您没有 运行 端口 21 上的 ftp 服务器,这是 FTP.

的默认端口

您随后提到的错误 60 是 CURLE_SSL_CACERT,这表明您已更进一步,但 libcurl 无法验证您的服务器。

我可以通过添加行

来解决问题

curl_easy_setopt(卷曲, CURLOPT_SSL_VERIFYPEER, 0);

应用程序现在不尝试验证 SSL 证书并继续连接