2017-05-13 3 views
0

ich C++ Anwendung haben, die zum Hochladen von Dateien hat. Wenn nicht auf einem Proxy, funktioniert der FTP-Upload großartig. Wenn der Client jedoch einen Proxy verwendet, kann LibCurl keine gute Anfrage erstellen. Zumindest weiß ich nicht, wie ich ihm die richtigen Informationen geben soll. Natürlich sage ich LibCurl die Proxy-Adresse, die bei HTTP-Anfragen sehr gut funktioniert. Der FTP-Upload schlägt jedoch fehl. Der Code Ich verwende:C++ Libcurl FTP hochladen über Proxy funktioniert nicht

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); 

Die Antwort, die ich erhalten:

* 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].com' 
> 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. 

Antwort

1

Sie können normalerweise nur tun, FTP-Uploads über einen HTTP-Proxy durch "Tunneln durch" es. Sie bitten libcurl dies zu tun, indem Sie die Option CURLOPT_HTTPPROXYTUNNEL setzen.

Ohne diese Option gesetzt, Libcurl Ihren FTP-Upload auf einen HTTP-PUT über den Proxy konvertieren.

Mit dieser Option gesetzt, libcurl eine CONNECT an den Proxy und fragen Sie nach einem Tunnel zum Zielserver ausgeben, und geben Sie dann „gewöhnliche FTP“ Befehle an den Server. Beachten Sie jedoch, dass viele HTTP-Proxies nicht so eingerichtet sind, dass CONNECT über Proxies zu anderen Ports als 443 oder vielleicht noch einigen mehr zugelassen wird. Port 21, der für eine typische FTP-Übertragung benötigt wird, wird selten akzeptiert.

+0

Vielen Dank für Ihre Antwort, so würde ich besser sein, von HTTP-Upload anstelle von FTP-Upload? Betrachten Sie die Leistung und allgemeine Akzeptanz von Proxies, die Port 80 unterstützen? –

+1

Über einen HTTP-Proxy, der eine viel höhere Erfolgsquote haben wird, ja. –