【问题标题】:Argument list too long libcurl c program error参数列表太长 libcurl c 程序错误
【发布时间】:2015-12-14 12:12:37
【问题描述】:

我正在使用 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 连接失败...参数列表太长:7"

即使我不添加CURLOPT_USE_SSL 部分也会得到相同的错误。

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

“FTP 连接失败...设备不是流:60”

有人知道解决方法吗?

【问题讨论】:

    标签: c ssl ftp libcurl


    【解决方案1】:

    您不能在 libcurl 的返回码上使用 strerror()。 libcurl 为其返回码提供了自己的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 未能验证您的服务器。

    【讨论】:

    • 我将 strerror 更改为 curl_easy_strerror。现在我收到了您提到的错误消息。但是端口号本身是 21,因为我正在使用 port21 连接到 filezilla 中的服务器。当我不在 URL 名称中使用 ftps 时,会出现此错误。当我添加 ftps 时出现错误 60。如何避免这种情况,因为我的机器在使用 filezilla 时验证了证书
    • 当您使用 FTPS 时,它默认使用另一个端口:990,我已经解释过您的 FTPS 会话失败,因为 libcurl 无法验证您的服务器证书。如果 filezilla 这样做了,那么让 libcurl 使用相同的 ca 包。我猜filezilla只是跳过了它......
    【解决方案2】:

    我可以通过添加行来解决问题

    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);

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

    【讨论】:

    • 从不为您在实验之外实际运行的代码禁用证书验证...
    猜你喜欢
    • 1970-01-01
    • 2015-10-30
    • 2019-11-15
    • 1970-01-01
    • 2015-06-25
    • 1970-01-01
    • 2020-05-08
    • 2011-03-12
    • 2015-03-12
    相关资源
    最近更新 更多