我试图模仿使用libcurl的python程序的行为并被服务器拒绝。尽管我可以说,这是文件处理方式的问题。这是python代码:
import requests
savedTextFile = open('filepath/file.txt', 'rw')
myPostRequest = requests.post("https://mywebserver.com/ws/v1/layers/template/lines2", headers={"API-Key":"source:1111111111"}, files={'file': savedTextFile}, verify=False)
我目前使用的libcurl / C ++代码是:
CURL* curl; //our curl object
struct curl_httppost* formpost=NULL;
struct curl_httppost* lastptr=NULL;
struct curl_slist* headerlist=NULL;
curl_global_init(CURL_GLOBAL_ALL); //pretty obvious
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "file", // required ?
CURLFORM_FILE, dbFile.c_str(), // makes this part a file upload part
CURLFORM_CONTENTTYPE, "application/octet-stream", // generic binary
CURLFORM_END);
curl = curl_easy_init();
const int MediumBufferSize = 256;
char sApiKeyHeaderLine[MediumBufferSize];
sprintf_s(sApiKeyHeaderLine, MediumBufferSize, "API-Key:%s", api.c_str());
headerlist = curl_slist_append((struct curl_slist*) headerlist, sApiKeyHeaderLine);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
curl_easy_setopt(curl, CURLOPT_URL, full_url.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); //tell curl to output its progress
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_easy_perform(curl);
curl_easy_cleanup(curl);
curl_global_cleanup();
不确定我缺少什么。这是DOS控制台输出:
* About to connect() to 54.219.209.180 port 443 (#1)
* Trying 54.219.209.180...
* Adding handle: conn: 0x490140
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 1 (0x490140) send_pipe: 1, recv_pipe: 0
* Connected to 54.219.209.180 (54.219.209.180) port 443 (#1)
* schannel: SSL/TLS connection with 54.219.209.180 port 443 (step 1/3)
* schannel: disable server certificate revocation checks
* schannel: using IP address, SNI is being disabled by disabling the servername check against the subject names in server certificates.
* schannel: verifyhost setting prevents Schannel from comparing the supplied tar get name with the subject names in server certificates. Also disables SNI.
* schannel: sending initial handshake data: sending 90 bytes...
* schannel: sent initial handshake data: sent 90 bytes
* schannel: SSL/TLS connection with 54.219.209.180 port 443 (step 2/3)
* schannel: failed to receive handshake, need more data
* schannel: SSL/TLS connection with 54.219.209.180 port 443 (step 2/3)
* schannel: encrypted data buffer: offset 797 length 4096
* schannel: sending next handshake data: sending 326 bytes...
* schannel: SSL/TLS connection with 54.219.209.180 port 443 (step 2/3)
* schannel: encrypted data buffer: offset 59 length 4096
* schannel: SSL/TLS handshake complete
* schannel: SSL/TLS connection with 54.219.209.180 port 443 (step 3/3)
* schannel: incremented credential handle refcount = 1
* schannel: stored credential handle in session cache
> POST /ws/v1/layers/template/lines1 HTTP/1.1 Host: 54.219.209.180 Accept: */* LatLongo-API-Key:source:1234567890
Content-Length: 35023 Expect: 100-continue Content-Type: multipart/form-data; boundary=------------------------3e5310b06e42 8f31
* schannel: client wants to read 16384 bytes
* schannel: encrypted data buffer: offset 0 length 16384
* schannel: encrypted data got 245
* schannel: encrypted data buffer: offset 245 length 16384
* schannel: decrypted data length: 209
* schannel: decrypted data added: 209
* schannel: decrypted data cached: offset 209 length 16384
* schannel: decrypted data buffer: offset 209 length 16384
* schannel: decrypted data returned 209
* schannel: decrypted data buffer: offset 0 length 16384
< HTTP/1.1 400 BAD REQUEST
< Date: Thu, 09 Jan 2014 04:04:59 GMT
< Content-Type: text/html; charset=utf-8
< Transfer-Encoding: chunked
< Connection: keep-alive
* Server gunicorn/18.0 is not blacklisted
< Server: gunicorn/18.0
* HTTP error before end of send, stop sending
< POST form invalid
* Closing connection 1
* schannel: shutting down SSL/TLS connection with 54.219.209.180 port 443
* schannel: clear security context handle
* schannel: decremented credential handle refcount = 0