2010-09-03 35 views
7

Vorrei inviare una richiesta di post http in C++. Sembra che libcurl (Curlpp) sia la strada da percorrere.C++ - come inviare una richiesta di post HTTP utilizzando Curlpp o libcurl

Ora, qui è una richiesta tipico che mando

http://abc.com:3456/handler1/start?<name-Value pairs> 

The name values pairs will have: 

field1: ABC 
field2: b, c, d, e, f 
field3: XYZ 

etc. 

Ora, vorrei sapere come ottenere lo stesso utilizzando curlpp o libcurl. Gli snippet di codice ti saranno di grande aiuto.

risposta

3

Non ho esperienza con Curlpp, ma è così che l'ho fatto con libcurl.

È possibile impostare il vostro URL di destinazione utilizzando

curl_easy_setopt(m_CurlPtr, CURLOPT_URL, "http://urlhere.com/"); 

valori POST vengono memorizzati in una lista collegata - si dovrebbe avere due variabili per contenere l'inizio e la fine di tale elenco in modo che cURL può aggiungere un valore ad esso.

struct curl_httppost* beginPostList; 
struct curl_httppost* endPostList; 

è quindi possibile aggiungere questa variabile post utilizzando

curl_formadd(&beginPostList, &endPostList, CURLFORM_COPYNAME, "key", CURLFORM_COPYCONTENTS, "value", CURLFORM_END); 

Invio quindi funziona così

curl_easy_setopt(m_CurlPtr, CURLOPT_POST, true); 
curl_easy_setopt(m_CurlPtr, CURLOPT_HTTPPOST, beginPostList); 
curl_easy_perform(m_CurlPtr); 

Spero che questo aiuti!

Problemi correlati