2017-05-13 54 views
0

我有一個C++應用程序,它必須上傳文件。當不在代理上時,FTP上傳效果很好。但是,當客戶端使用代理上傳時,LibCurl無法建立良好的請求。至少,我不知道如何給它正確的信息。當然,我會告訴LibCurl這個代理地址,它可以很好地處理HTTP請求。但是,FTP上傳失敗。我正在使用的代碼:C++ libcurl 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 '[email protected]' 
> PUT ftp://[email protected]:[email protected]/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 [email protected] 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. 

回答

1

你通常只能「通過隧道」,它做FTP上傳了HTTP代理服務器來。您可以通過設置CURLOPT_HTTPPROXYTUNNEL選項來讓libcurl執行此操作。

如果沒有該選項設置,libcurl會通過代理將您的FTP上傳轉換爲HTTP PUT。

使用該選項設置,libcurl將向代理髮出CONNECT並請求到目標服務器的隧道,然後向服務器發出「普通FTP」命令。但請注意,許多HTTP代理未設置爲允許通過代理連接到除443之外的其他端口,或者更多。很少接受典型FTP傳輸所需的端口21。

+0

感謝您的回覆,所以我會更好地使用HTTP上傳,而不是FTP上傳?查看支持80端口的代理的性能和普遍接受度? –

+1

通過HTTP代理將有更高的成功率,是的。 –