【问题标题】:C++ libcurl FTP upload over proxy not workingC ++ libcurl FTP通过代理上传不起作用
【发布时间】:2017-05-13 22:50:02
【问题描述】:

我有一个 C++ 应用程序,它必须上传文件。不在代理上时,FTP 上传效果很好。但是,当客户端使用代理上传时,LibCurl 无法构建好的请求。至少,我不知道如何给它正确的信息。当然,我告诉 LibCurl 代理地址,它对 HTTP 请求非常有效。但是,FTP 上传失败。我正在使用的代码:

struct curl_slist *LibcurlHeaders = NULL;
curl = curl_easy_init();
string ProxyAddress = "ProxyURL";
string JSONFileName = "dataonserver.json";
FILE* JSONFile = fopen("data.json", "rb");
if (curl) {
     CurlResponse = "";
     host = "ftp://host.com/" + JSONFileName;
     if (ProxyAddress.length() > 0) {
          curl_easy_setopt(curl, CURLOPT_PROXY, ProxyAddress.c_str());
          }
     curl_easy_setopt(curl, CURLOPT_URL, host.c_str());
     curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
     curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER , 1);
     curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST , 1);
     LibcurlHeaders = curl_slist_append(LibcurlHeaders, "Expect:");
     curl_easy_setopt(curl, CURLOPT_USERPWD, (FTPUsername + ":" + FTPPassword).c_str());
     curl_easy_setopt(curl, CURLOPT_CAINFO, FilePathSSLCertificate.c_str());
     curl_easy_setopt(curl, CURLOPT_HTTPHEADER, LibcurlHeaders);
     curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
     curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
     curl_easy_setopt(curl, CURLOPT_READDATA, JSONFile);
     res = curl_easy_perform(curl);
     if (res != CURLE_OK) {
          LibcurlError(curl_easy_strerror(res), host);
          }
     curl_slist_free_all(LibcurlHeaders);
     curl_easy_cleanup(curl);
     }
fclose(JSONFile);

我得到的回应:

* timeout on name lookup is not supported
*   Trying host IP...
* TCP_NODELAY set
* Connected to host (host IP) port 21 (#0)
* Server auth using Basic with user 'username@domain.com'
> PUT ftp://username@domain.com:password@domain.com/FileName HTTP/1.1
Host: domain.com:21
Authorization: Basic Q2xpZW50VXBsb2FkQGdvZmlsZXIub3JnOkNsaWVudFVwbG9hZDE=
Accept: */*
Proxy-Connection: Keep-Alive
Transfer-Encoding: chunked

220-  This is the xxxx FTP proxy server.
220-  My external IP address is xxxx and I have
220-  a backup ftp proxy in xxxx. When setting up
220-  access to 3rd parties, be sure to allow both source
220-  addresses.
220-
220-  All requests will come from one of the sources IP's below:
220-   xxxxx (xxx Proxy)
220-   xxxxx (xxx Proxy)
220-
220-  To connect to a site:
220-
220-  Enter the username and site name at the User: prompt.
220-  For example, to log into ftp.uu.net as anonymous, you
220-  would give anonymous@ftp.uu.net at the User: prompt.
220-  Enter the site password at the Password: prompt.
220-  If you are connecting as anonymous, give your email address.
220-
220   Type quit to disconnect.
500 Syntax error, command unrecognized.
500 Syntax error, command unrecognized.
500 Syntax error, command unrecognized.
500 Syntax error, command unrecognized.
500 Syntax error, command unrecognized.
500 Syntax error, command unrecognized.
500 Syntax error, command unrecognized.
500 Syntax error, command unrecognized.
500 Syntax error, command unrecognized.

【问题讨论】:

    标签: c++ file-upload proxy ftp libcurl


    【解决方案1】:

    您通常只能通过“隧道”通过 HTTP 代理进行 FTP 上传。您可以通过设置 CURLOPT_HTTPPROXYTUNNEL 选项来要求 libcurl 执行此操作。

    如果没有设置该选项,libcurl 会将您的 FTP 上传转换为通过代理的 HTTP PUT。

    设置该选项后,libcurl 将向代理发出 CONNECT 并请求到目标服务器的隧道,然后向服务器发出“普通 FTP”命令。但是请注意,许多 HTTP 代理未设置为允许通过代理连接到 443 或更多端口以外的其他端口。典型 FTP 传输所需的端口 21 很少被接受。

    【讨论】:

    • 感谢您的回复,所以我最好使用HTTP-upload而不是FTP-upload?查看支持端口 80 的代理的性能和普遍接受度?
    • 通过 HTTP 代理,成功率更高,是的。
    猜你喜欢
    • 1970-01-01
    • 2023-03-14
    • 2015-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多