• curl 的简单使用


    #if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
    #include "curl/include/win32/curl/curl.h"
    #elif CC_TARGET_PLATFORM == CC_PLATFORM_IOS
    #include "curl/include/ios/curl/curl.h"
    #elif CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
    #include "curl/include/android/curl/curl.h"
    #endif

    //下载

      CURL *pCurl;   

      CURLcode nResCode;   

     pCurl = curl_easy_init();//初始化CURL取得初始化成功后的CURL指针   

     if (pCurl != NULL)   {

        curl_easy_setopt(pCurl, CURLOPT_URL, szFileUrl.c_str());    

      if (pFile != NULL) curl_easy_setopt(pCurl, CURLOPT_FILE, pFile); //指定写入的文件指针    

        curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, WriteCallback);  //设置写数据的回调函数    

          curl_easy_setopt(pCurl, CURLOPT_VERBOSE, TRUE);      //让CURL报告每一件意外的事情    

          curl_easy_setopt(pCurl, CURLOPT_TIMEOUT, 60);      //设置超时时间    

          curl_easy_setopt(pCurl, CURLOPT_NOPROGRESS, 0L);    

          curl_easy_setopt(pCurl, CURLOPT_PROGRESSFUNCTION, DownProgresss); //指定显示进度的回调函数   

          curl_easy_setopt(pCurl, CURLOPT_NOSIGNAL, 1L);   

          curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYPEER, false);    

          nResCode = curl_easy_perform(pCurl);        //执行上面设定的动作并返回状态码   

          curl_easy_cleanup(pCurl);           //释放相关资源    

        fclose(pFile);   

    }

      //释放全局curl对象   

       curl_global_cleanup();

      //更新   

      if (nResCode == 0)   {

          Android_RunAPK(szSavePath.c_str());    

          GetFrameManager()->SetRootEnded();   

       }

    //上传

     CURL *curl;  CURLcode res;

     struct curl_httppost *formpost = NULL;  

    struct curl_httppost *lastptr = NULL;  

    struct curl_slist *headerlist = NULL;  

    static const char buf[] = "Expect:";

     curl_global_init(CURL_GLOBAL_ALL);

     /* Fill in the file upload field */  

    CURLFORMcode formcode = curl_formadd(&formpost,   

      &lastptr,   

      CURLFORM_COPYNAME, "FileUpLoad",   

      CURLFORM_FILE, szFileUrl.c_str(),  //本地待上传文件,完整路径   

      CURLFORM_END);

     /* Fill in the filename field */  

    formcode = curl_formadd(&formpost,   

      &lastptr,   

      CURLFORM_COPYNAME, "SaveFileN",   

      CURLFORM_COPYCONTENTS,

       szSaveN.c_str(),  //在http服务器保存的名字   

      CURLFORM_END);

     /* Fill in the submit field too, even if this is rarely needed */  

    formcode = curl_formadd(&formpost,   

      &lastptr,   

      CURLFORM_COPYNAME, "submit",   

      CURLFORM_COPYCONTENTS, "Submit",   

      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 (curl)  {  

       /* what URL that receives this POST */   

      std::string strWebUrl = StringUtils::format("%s/Default.aspx", GetProject()->GetWebUrl());

        /* what URL that receives this POST */   

      curl_easy_setopt(curl, CURLOPT_URL, strWebUrl.c_str());   

      curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);                           

      curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);   

      curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);

      res = curl_easy_perform(curl);   

      /* Check for errors */   

      if (res != CURLE_OK) fprintf(stderr, "curl_easy_perform() failed: %s ",curl_easy_strerror(res));  

       /* always cleanup */   

      curl_easy_cleanup(curl);   

      /* then cleanup the formpost chain */   

      curl_formfree(formpost);   

      /* free slist */   

      curl_slist_free_all(headerlist);  

    }

  • 相关阅读:
    Python数据分析与机器学习-Matplot_2
    Python数据分析与机器学习-Matplot_1
    1008. 数组元素循环右移问题 (20)
    Latex小技巧
    执行PowerShell脚本的时候出现"在此系 统上禁止运行脚本"错误
    Linux使用MentoHust联网线上校园网, 回到普通有线网络却连不上?
    Re:uxul
    Linux下nautilus的右键快捷菜单项设置
    从入门到入狱——搭讪技巧
    Latex命令
  • 原文地址:https://www.cnblogs.com/liugangBlog/p/6768077.html
Copyright © 2020-2023  润新知