1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
   |  
#include <curl/curl.h>
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <sstream>
 
char* szFileContent(char* szFilePath)
{
	std::ifstream ifsFile(szFilePath);
	std::stringstream ssBuffer;
 
	if (ifsFile)
	{
		ssBuffer<< ifsFile.rdbuf();
		ifsFile.close();
		return buffer.str();
	}
}
 
int iLaunchFlow(char* szURL, char* szUser, char* szPassword, char* szFilePath)
{
	// Initialisation de CURL
	CURL* cCurl = curl_easy_init();
	if(!cCurl) return -1;
 
	// Ajout des paramètres HTTP
	curl_easy_setopt(cCurl, CURLOPT_URL, szURL);					
	char szUserpassword;
	sprintf(szUserpassword, "%s:%s", szUser, szPassword);
	curl_easy_setopt(cCurl, CURLOPT_USERPWD, szUserpassword);		
 
	// Copie du contenu du fichier dans le contenu du POST
	char* szBuffer = szFileContent(szFilePath);
	curl_easy_setopt(cCurl, CURLOPT_POST, 1);					
	curl_easy_setopt(cCurl, CURLOPT_POSTFIELDS, szBuffer);				
	curl_easy_setopt(cCurl, CURLOPT_POSTFIELDSIZE, strlen (szBuffer));	
 
	// Ne pas vérifier le certificat ni le hostname
	curl_easy_setopt(cCurl, CURLOPT_SSL_VERIFYPEER, 0);				
	curl_easy_setopt(cCurl, CURLOPT_SSL_VERIFYHOST, 0);				
 
	// POST HTTP proprement dit
	CURLcode cRetVal = curl_easy_perform(cCurl);
 
	// Récupération du code retour HTTP
	int iRetCode;
	curl_easy_getinfo (cCurl, CURLINFO_HTTP_CODE, &iRetCode);		
 
	// Nettoyage
	curl_easy_cleanup(cCurl);
	delete szBuffer;
 
	return iRetCode;
}
 
int main(void)
{
	return iLaunchFlow("http://www.developpez.com:1234", "user", "pass", "C:\\Temp\\test.xml");
} | 
Partager