postit2.c
上传用户:coffee44
上传日期:2018-10-23
资源大小:12304k
文件大小:3k
源码类别:

TAPI编程

开发平台:

Visual C++

  1. /*****************************************************************************
  2.  *                                  _   _ ____  _
  3.  *  Project                     ___| | | |  _ | |
  4.  *                             / __| | | | |_) | |
  5.  *                            | (__| |_| |  _ <| |___
  6.  *                             ___|___/|_| ______|
  7.  *
  8.  * $Id: postit2.c,v 1.5 2007-07-12 21:11:10 danf Exp $
  9.  *
  10.  * Example code that uploads a file name 'foo' to a remote script that accepts
  11.  * "HTML form based" (as described in RFC1738) uploads using HTTP POST.
  12.  *
  13.  * The imaginary form we'll fill in looks like:
  14.  *
  15.  * <form method="post" enctype="multipart/form-data" action="examplepost.cgi">
  16.  * Enter file: <input type="file" name="sendfile" size="40">
  17.  * Enter file name: <input type="text" name="filename" size="30">
  18.  * <input type="submit" value="send" name="submit">
  19.  * </form>
  20.  *
  21.  * This exact source code has not been verified to work.
  22.  */
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <curl/curl.h>
  26. #include <curl/types.h>
  27. #include <curl/easy.h>
  28. int main(int argc, char *argv[])
  29. {
  30.   CURL *curl;
  31.   CURLcode res;
  32.   struct curl_httppost *formpost=NULL;
  33.   struct curl_httppost *lastptr=NULL;
  34.   struct curl_slist *headerlist=NULL;
  35.   static const char buf[] = "Expect:";
  36.   curl_global_init(CURL_GLOBAL_ALL);
  37.   /* Fill in the file upload field */
  38.   curl_formadd(&formpost,
  39.                &lastptr,
  40.                CURLFORM_COPYNAME, "sendfile",
  41.                CURLFORM_FILE, "postit2.c",
  42.                CURLFORM_END);
  43.   /* Fill in the filename field */
  44.   curl_formadd(&formpost,
  45.                &lastptr,
  46.                CURLFORM_COPYNAME, "filename",
  47.                CURLFORM_COPYCONTENTS, "postit2.c",
  48.                CURLFORM_END);
  49.   /* Fill in the submit field too, even if this is rarely needed */
  50.   curl_formadd(&formpost,
  51.                &lastptr,
  52.                CURLFORM_COPYNAME, "submit",
  53.                CURLFORM_COPYCONTENTS, "send",
  54.                CURLFORM_END);
  55.   curl = curl_easy_init();
  56.   /* initalize custom header list (stating that Expect: 100-continue is not
  57.      wanted */
  58.   headerlist = curl_slist_append(headerlist, buf);
  59.   if(curl) {
  60.     /* what URL that receives this POST */
  61.     curl_easy_setopt(curl, CURLOPT_URL, "http://curl.haxx.se/examplepost.cgi");
  62.     if ( (argc == 2) && (!strcmp(argv[1], "noexpectheader")) )
  63.       /* only disable 100-continue header if explicitly requested */
  64.       curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
  65.     curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
  66.     res = curl_easy_perform(curl);
  67.     /* always cleanup */
  68.     curl_easy_cleanup(curl);
  69.     /* then cleanup the formpost chain */
  70.     curl_formfree(formpost);
  71.     /* free slist */
  72.     curl_slist_free_all (headerlist);
  73.   }
  74.   return 0;
  75. }