int http_post_file (const char *url, const char *user, const char *pwd, const char *filename)
{
ASSERT (URL! = NULL);
ASSERT (user! = NULL);
ASSERT (pwd = NULL);
ASSERT (filename! = NULL);
int ret =-1;
CURL *curl = NULL;
Curlcode Code;
Curlformcode FormCode;
int timeout = 15;
#define CHECK_FORM_ERROR (x)/
if ((FormCode = (x))! = CURL_FORMADD_OK)/
{ /
fprintf (stderr, "curl_formadd[%d] error./n", formcode); /
Goto out; /
}
#define CHECK_SETOPT_ERROR (x)/
if ((code = (x))! = CURLE_OK)/
{ /
fprintf (stderr, "curl_easy_setopt[%d] error./n", code); /
Goto all; /
}
struct Curl_httppost *post=null;
struct Curl_httppost *last=null;
struct Curl_slist *headerlist=null;
Check_form_error (Curl_formadd (&post, &last, Curlform_copyname, "user",
curlform_copycontents, User,
Curlform_end));
Check_form_error (Curl_formadd (&post, &last, Curlform_copyname, "password",
Curlform_copycontents, PWD,
Curlform_end));
Check_form_error (Curl_formadd (&post, &last, Curlform_copyname, "file",
Curlform_file, filename,
Curlform_end));
Check_form_error (Curl_formadd (&post, &last,
Curlform_copyname, "Submit",
Curlform_copycontents, "Upload",
Curlform_end));
Curl = Curl_easy_init ();
if (curl = = NULL)
{
fprintf (stderr, "Curl_easy_init () error./n");
Goto out;
}
Check_setopt_error (curl_easy_setopt (Curl, Curlopt_header, 0));
Check_setopt_error (curl_easy_setopt (Curl, Curlopt_url, URL));
Check_setopt_error (curl_easy_setopt (Curl, curlopt_httppost, post));
Check_setopt_error (curl_easy_setopt (Curl, Curlopt_timeout, TIMEOUT));
Code = curl_easy_perform (curl);
if (Code! = CURLE_OK)
{
fprintf (stderr, "curl_easy_perform[%d] error./n", code);
Goto all;
}
ret = 0;
All
Curl_easy_cleanup (curl);
Out
Curl_formfree (POST);
return ret;
}
Curl's HTTP Upload file code