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

TAPI编程

开发平台:

Visual C++

  1. /*****************************************************************************
  2.  *                                  _   _ ____  _
  3.  *  Project                     ___| | | |  _ | |
  4.  *                             / __| | | | |_) | |
  5.  *                            | (__| |_| |  _ <| |___
  6.  *                             ___|___/|_| ______|
  7.  *
  8.  * $Id: multi-post.c,v 1.6 2008-05-22 21:20:09 danf Exp $
  9.  *
  10.  * This is an example application source code using the multi interface
  11.  * to do a multipart formpost without "blocking".
  12.  */
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <sys/time.h>
  16. #include <curl/curl.h>
  17. int main(int argc, char *argv[])
  18. {
  19.   CURL *curl;
  20.   CURLM *multi_handle;
  21.   int still_running;
  22.   struct curl_httppost *formpost=NULL;
  23.   struct curl_httppost *lastptr=NULL;
  24.   struct curl_slist *headerlist=NULL;
  25.   static const char buf[] = "Expect:";
  26.   /* Fill in the file upload field. This makes libcurl load data from  
  27.      the given file name when curl_easy_perform() is called. */
  28.   curl_formadd(&formpost,
  29.                &lastptr,
  30.                CURLFORM_COPYNAME, "sendfile",
  31.                CURLFORM_FILE, "postit2.c",
  32.                CURLFORM_END);
  33.   /* Fill in the filename field */
  34.   curl_formadd(&formpost,
  35.                &lastptr,
  36.                CURLFORM_COPYNAME, "filename",
  37.                CURLFORM_COPYCONTENTS, "postit2.c",
  38.                CURLFORM_END);
  39.   /* Fill in the submit field too, even if this is rarely needed */
  40.   curl_formadd(&formpost,
  41.                &lastptr,
  42.                CURLFORM_COPYNAME, "submit",
  43.                CURLFORM_COPYCONTENTS, "send",
  44.                CURLFORM_END);
  45.   curl = curl_easy_init();
  46.   multi_handle = curl_multi_init();
  47.   /* initalize custom header list (stating that Expect: 100-continue is not
  48.      wanted */
  49.   headerlist = curl_slist_append(headerlist, buf);
  50.   if(curl && multi_handle) {
  51.     /* what URL that receives this POST */
  52.     curl_easy_setopt(curl, CURLOPT_URL,
  53.                      "http://www.fillinyoururl.com/upload.cgi");
  54.     curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  55.     curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
  56.     curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
  57.     curl_multi_add_handle(multi_handle, curl);
  58.     while(CURLM_CALL_MULTI_PERFORM ==
  59.           curl_multi_perform(multi_handle, &still_running));
  60.     while(still_running) {
  61.       struct timeval timeout;
  62.       int rc; /* select() return code */
  63.       fd_set fdread;
  64.       fd_set fdwrite;
  65.       fd_set fdexcep;
  66.       int maxfd;
  67.       FD_ZERO(&fdread);
  68.       FD_ZERO(&fdwrite);
  69.       FD_ZERO(&fdexcep);
  70.       /* set a suitable timeout to play around with */
  71.       timeout.tv_sec = 1;
  72.       timeout.tv_usec = 0;
  73.       /* get file descriptors from the transfers */
  74.       curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
  75.       /* In a real-world program you OF COURSE check the return code of the
  76.          function calls, *and* you make sure that maxfd is bigger than -1
  77.          so that the call to select() below makes sense! */
  78.       rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
  79.       switch(rc) {
  80.       case -1:
  81.         /* select error */
  82.         break;
  83.       case 0:
  84.         printf("timeout!n");
  85.       default:
  86.         /* timeout or readable/writable sockets */
  87.         printf("perform!n");
  88.         while(CURLM_CALL_MULTI_PERFORM ==
  89.               curl_multi_perform(multi_handle, &still_running));
  90.         printf("running: %d!n", still_running);
  91.         break;
  92.       }
  93.     }
  94.     curl_multi_cleanup(multi_handle);
  95.     /* always cleanup */
  96.     curl_easy_cleanup(curl);
  97.     /* then cleanup the formpost chain */
  98.     curl_formfree(formpost);
  99.     /* free slist */
  100.     curl_slist_free_all (headerlist);
  101.   }
  102.   return 0;
  103. }