【问题标题】:LibCurl: Changing CWDLibCurl:更改 CWD
【发布时间】:2023-12-04 15:58:01
【问题描述】:

我正在从 wxWidgets 附带的 wxFTP 迁移到 libcurl。我有一种方法可以通过简单地提供 URL 来连接并获取 / 目录中的所有目录和文件。现在我需要切换到另一个目录(比如说 /public_html/mysite)。我不能用 libcurl 做到这一点。

我不敢相信在 libcurl 中做到这一点是如此困难,所以我倾向于得出结论,我在这里错过了一些东西。我的相关代码如下所示,我所拥有的只是像 /www 这样的相对路径,而不是像 ftp://someurl.com/www/ 这样的 ftp URL

很抱歉,如果它对 libcurl 来说是非常新的东西并且已经工作了很多小时已经不成功了。

CURL* handle = curl_easy_init();
SetHandleOptions(handle); //set options
CURLcode res;

if(handle)
{
    wxString command ="CWD "+path;
    curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST , command.ToStdString().c_str());  
    res = curl_easy_perform(handle);

    if(res==CURLE_OK)
    { 

        wxString str(curl_easy_strerror(res));
        SendMessage(str);

        curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST , "MLSD");
        res = curl_easy_perform(handle);

        if(res==CURLE_OK)
        {
            wxString str(curl_easy_strerror(res));
            SendMessage(str);
        }
        else
        {
            //send error message
            wxString str(curl_easy_strerror(res));
            SendMessage(str);
        } 
    }
    else
    {
        //send error message
        wxString str(curl_easy_strerror(res));
        SendMessage(str);
    }
}
curl_easy_cleanup(handle);

【问题讨论】:

    标签: c++ ftp wxwidgets libcurl


    【解决方案1】:

    这是我的错误,因为代码结束了发送格式为ftp://ftp.hosanna.site40.net:21/public_html 的 URL,所以我在 easy setopt 中改为使用 PORT。

    所以这不是 curl 的问题,而是我的问题(虽然对我来说非常微妙) 还要确保所有目录都以 / 结尾,否则 libray 会将它们解释为文件。

    例如

    ftp://ftp.hosanna.site40.net/public_html //interpreted as file
    ftp://ftp.hosanna.site40.net/public_html/ //interpreted as directory
    

    【讨论】: