curl-library
Issue With curl_easy_perform
Date: Fri, 31 Mar 2006 16:39:27 -0800
Hello All:
I am trying to use libcurl to do a simple http_post, I have the code below,
everything works like a charm except that curl_easy_perform keeps popping up
this window, I have a callback function that I have written using
CURLOPT_WRITEFUNCTION but that still does not make the dos window completely
go away, any help on this would be much appreciated. I have been struggling
with this for a day or so, any ideas??
/* libcurl callback function */
extern "C" size_t write_data(void *source ,size_t size ,size_t nmemb ,void
*userData);
extern "C" size_t write_data(void *source ,size_t size ,size_t nmemb ,void
*userData)
{
size_t retval=0;
std::cout<<"Inside write_data\n";
return retval;
}
int MyClass::uploadFile(std::string URL,std::string path)
{
CURL *curl;
CURLcode res;
struct curl_httppost *formpost=NULL;
struct curl_httppost *lastptr=NULL;
struct curl_slist *headerlist=NULL;
char buf[] = "Expect:";
bool delFile = false;
curl_global_init(CURL_GLOBAL_ALL);
/* Fill in the file upload field */
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "sendfile",
CURLFORM_FILE, path.c_str(),
CURLFORM_END);
/* Fill in the filename field */
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "filename",
CURLFORM_COPYCONTENTS, path.c_str(),
CURLFORM_END);
/* Fill in the submit field too, even if this is rarely needed */
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "submit",
CURLFORM_COPYCONTENTS, "send",
CURLFORM_END);
curl = curl_easy_init();
/* initalize custom header list (stating that Expect: 100-continue is not
wanted */
headerlist = curl_slist_append(headerlist, buf);
/* if we have a handle proceed with the transaction */
if(curl)
{
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_data);
/* what URL that receives this POST */
curl_easy_setopt(curl, CURLOPT_URL, URL.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
res = curl_easy_perform(curl);
/* if the file was transported successfully we need to delete
the file else wait a while and try to upload
*/
if (res==CURLE_OK)
delFile=true;
else
{
/* if we are here we wait
and try the upload again till we are successful
for now we set this to 2 minutes
later it will be configurable
*/
Sleep(120000);
uploadFile(URL,path);
}
/* cleanup */
curl_easy_cleanup(curl);
/* then cleanup the formpost chain */
curl_formfree(formpost);
/* free slist */
curl_slist_free_all (headerlist);
}
if (delFile)
DeleteLog(path);
return res;
}
Thanks Again
Received on 2006-04-01