C++ libcurl FTP 上传通过代理不起作用

C++ libcurl FTP upload over proxy not working

本文关键字:代理 不起作用 libcurl FTP C++      更新时间:2023-10-16

我有一个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.

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

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

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