xcode如何添加libCurl


[cpp]
  view plain copy
  1. #include <stdio.h>  
  2. #include <string.h>  
  3.   
  4. //引入CURL类库  
  5. #include <curl/curl.h>  
  6. #include <sys/time.h>  
  7. #include <unistd.h>  
  8.   
  9. //引入多线程类库  
  10. #include <pthread.h>  
  11.   
  12. #include "JsonBox.h"  
  13.   
  14. char *buffer = NULL;  
  15.   
  16. size_t write_data(char *ptr,size_t size,size_t nmemb,void *userData)  
  17. {  
  18.     static int flag = 1;  
  19.     FILE *fp = fopen("/Users/kevin/Desktop/test.text""a");  
  20.     char *data = NULL;  
  21.     buffer = ptr;  
  22.     data = ptr;  
  23.   
  24.     //strcpy((char *)ptr,(char *)data);  
  25.     int writeen = fwrite(ptr, size, nmemb, fp);  
  26.     //CCLog("close");  
  27.     printf("write_data 第%d次\n",flag++);  
  28.     printf("写入字节数量是 %d\n",writeen);  
  29.     printf("data**********:%s",data);  
  30.     putchar(10);  
  31.     printf("*********************************************\n");  
  32.     fclose(fp);  
  33.     return written;  
  34. }  
  35.   
  36. int main(int argc, const char * argv[])  
  37. {  
  38.     char *serverIP = "http://192.168.1.112/jforum?module=shoot&action=start";  
  39.     char *serverIP2 = "http://weather.china.xappengine.com/api?city=beijing";  
  40.     char *serverIP3 = "http://192.168.1.105/jforum?module=shoot&action=start";  
  41.       
  42.     //1.同步请求http,并且使用CURLOPT_FOLLOWLOCATION获取返回数据  
  43.     /* 
  44.     CURL *curl = curl_easy_init(); 
  45.     CURLcode res; 
  46.      
  47.     if (curl) { 
  48.         curl_easy_setopt(curl,CURLOPT_URL, "http://esample.com"); 
  49.         curl_easy_setopt(curl,CURLOPT_FOLLOWLOCATION, 1L); 
  50.         res = curl_easy_perform(curl); 
  51.       
  52.         if (res != CURLE_OK) { 
  53.             fprintf(stderr, "curl_easy_perform() failed:%s\n",curl_easy_strerror(res)); 
  54.         } 
  55.         curl_easy_cleanup(curl); 
  56.      
  57.     }*/  
  58.       
  59.     //2.使用同步方式向服务器发送数据  
  60.     /*CURL *curl; 
  61.     CURLcode res; 
  62.     const char *postThis = "name=Kevin&passWord=1234"; 
  63.     curl = curl_easy_init(); 
  64.     if (curl) { 
  65.         curl_easy_setopt(curl, CURLOPT_URL, serverIP2); 
  66.         curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postThis); 
  67.         curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE,(long)strlen(postThis)); 
  68.         res = curl_easy_perform(curl); 
  69.         printf("%d\n",res); 
  70.         if (res==CURLE_COULDNT_CONNECT) { 
  71.             printf("连接服务器失败!\n"); 
  72.         } 
  73.         printf(""); 
  74.         if (res!=CURLE_OK) { 
  75.             //如果返回错误,通过此方法输出错误信息 
  76.             fprintf(stderr, "curl_easy_perform() failed :%s",curl_easy_strerror(res)); 
  77.         } 
  78.         curl_easy_cleanup(curl); 
  79.     } 
  80.     putchar(10);*/  
  81.       
  82.     //3.https协议的简单请求  
  83.     /*CURL *curl; 
  84.     //CURLcode  0表示正常 
  85.     CURLcode res; 
  86.     //全局初始化变量,但不是线程安全,注意使用 
  87.     curl_global_init(CURL_GLOBAL_DEFAULT); 
  88.     curl =curl_easy_init(); 
  89.     if (curl) { 
  90.         curl_easy_setopt(curl, CURLOPT_URL, "http://example.com"); 
  91.          
  92. #ifdef SKIP_PEER_VERIFICATION 
  93.         curl_easy_setopt(CURL, CURLOPT_SSL_VERIFYPEER, 0L); 
  94. #endif 
  95.          
  96. #ifdef SKIP_HOSTNAME_VERIFICATION 
  97.         curl_easy_setopt(CURL, CURLOPT_SSL_VERIFYHOST, 0L); 
  98. #endif 
  99.         res = curl_easy_perform(curl); 
  100.         if (res!=CURLE_OK) { 
  101.             fprintf(stderr, "failed :%s",curl_easy_strerror(res)); 
  102.         } 
  103.         curl_easy_cleanup(curl); 
  104.          
  105.     } 
  106.     curl_global_cleanup();*/  
  107.       
  108.     //4.异步请求2个http  
  109.     /* 
  110.      
  111.     CURL *http1,*http2; 
  112.     CURLcode res1,res2; 
  113.     CURLM *multi; 
  114.     int still_running; 
  115.     http1 = curl_easy_init(); 
  116.     http2 = curl_easy_init(); 
  117.     curl_easy_setopt(http1, CURLOPT_URL, "http://www.example.com"); 
  118.     curl_easy_setopt(http2, CURLOPT_URL, "http://loaclhost"); 
  119.     multi = curl_multi_init(); 
  120.     curl_multi_add_handle(multi, http1); 
  121.     curl_multi_add_handle(multi, http2); 
  122.     curl_multi_perform(multi, &still_running); 
  123.     do { 
  124.         struct timeval timeout; 
  125.         int rc; 
  126.         fd_set fdread; 
  127.         fd_set fdwrite; 
  128.         fd_set fdexcep; 
  129.         int maxfd = -1; 
  130.         long curl_timeo = -1; 
  131.         FD_ZERO(&fdread); 
  132.         FD_ZERO(&fdwrite); 
  133.         FD_ZERO(&fdexcep); 
  134.         timeout.tv_sec = 1; 
  135.         timeout.tv_usec = 0; 
  136.         curl_multi_timeout(multi, &curl_timeo); 
  137.         if (curl_timeo >= 0) 
  138.         { 
  139.             timeout.tv_sec = curl_timeo /1000; 
  140.             if (timeout.tv_sec > 1) 
  141.             { 
  142.                 timeout.tv_sec = 1; 
  143.             } 
  144.             else 
  145.             { 
  146.                 timeout.tv_usec = (curl_timeo % 1000) * 1000; 
  147.             } 
  148.         } 
  149.         curl_multi_fdset(multi, &fdread, &fdwrite, &fdexcep,&maxfd); 
  150.         rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout); 
  151.         switch (rc) { 
  152.             case -1: 
  153.                  
  154.                 break; 
  155.                  
  156.             default: 
  157.                 curl_multi_perform(multi, &still_running); 
  158.                 break; 
  159.         } 
  160.     } while (still_running); 
  161.     curl_multi_cleanup(multi); 
  162.     curl_easy_cleanup(http1); 
  163.     curl_easy_cleanup(http2); 
  164.      
  165.      
  166.     putchar(10); 
  167.     printf("Hello, World!\n"); 
  168.     CURL *curl1; 
  169.     curl1 = curl_easy_init(); 
  170.     curl_easy_setopt(curl1, CURLOPT_URL, "http://www.google.com"); 
  171.     curl_easy_setopt(curl1, CURLOPT_FOLLOWLOCATION,1L); 
  172.     curl_easy_perform(curl1);*/  
  173.       
  174.     //5.CURLOPT_POSTFIELDS  
  175.     //char *buffer = NULL;  
  176.     //char *userName = "my name is Kevin";  
  177.       
  178.     CURL *curl5;  
  179.     char *data = "name=Kevin&passWord=5678";  
  180.     curl5 = curl_easy_init();  
  181.     curl_easy_setopt(curl5, CURLOPT_URL, serverIP2);  
  182.     //curl_easy_setopt(curl5, CURLOPT_POSTFIELDS, data);  
  183.     curl_easy_setopt(curl5, CURLOPT_WRITEFUNCTION, write_data);  
  184.     //curl_easy_setopt(curl5, CURLOPT_FOLLOWLOCATION, 1L);  
  185.     //curl_easy_setopt(curl5, CURLOPT_PASSWORD,"1234");  
  186.     //curl_easy_setopt(curl5, CURLOPT_USERNAME, "kevin");  
  187.     //curl_easy_setopt(curl5, CURLOPT_WRITEDATA, &buffer);  
  188.       
  189.     curl_easy_perform(curl5);  
  190.     curl_easy_cleanup(curl5);  
  191.     putchar(10);  
  192.     printf("buffer*******:%s\n",buffer);  
  193.       
  194.     //printf("userName  %s\n",userName);  
  195.       
  196.   
  197.     //6.json包获取http  get 方法  
  198.     /* 
  199.      CURLOPT_WRITEFUNCTION 
  200.       
  201.      Pass a pointer to a function that matches the following prototype: size_t function( char *ptr, size_t size, size_t nmemb, void *userdata); This function gets called by libcurl as soon as there is data received that needs to be saved. The size of the data pointed to by ptr is size multiplied with nmemb, it will not be zero terminated. Return the number of bytes actually taken care of. If that amount differs from the amount passed to your function, it'll signal an error to the library. This will abort the transfer and return CURLE_WRITE_ERROR. 
  202.       
  203.      From 7.18.0, the function can return CURL_WRITEFUNC_PAUSE which then will cause writing to this connection to become paused. See curl_easy_pause(3) for further details. 
  204.       
  205.      This function may be called with zero bytes data if the transferred file is empty. 
  206.       
  207.      Set this option to NULL to get the internal default function. The internal default function will write the data to the FILE * given with CURLOPT_WRITEDATA. 
  208.       
  209.      Set the userdata argument with the CURLOPT_WRITEDATA option. 
  210.       
  211.      The callback function will be passed as much data as possible in all invokes, but you cannot possibly make any assumptions. It may be one byte, it may be thousands. The maximum amount of body data that can be passed to the write callback is defined in the curl.h header file: CURL_MAX_WRITE_SIZE (the usual default is 16K). If you however have CURLOPT_HEADER set, which sends header data to the write callback, you can get up to CURL_MAX_HTTP_HEADER bytes of header data passed into it. This usually means 100K. 
  212.       
  213.      CURLOPT_WRITEDATA 
  214.       
  215.      Data pointer to pass to the file write function. If you use the CURLOPT_WRITEFUNCTION option, this is the pointer you'll get as input. If you don't use a callback, you must pass a 'FILE *' (cast to 'void *') as libcurl will pass this to fwrite() when writing data. By default, the value of this parameter is unspecified. 
  216.       
  217.      The internal CURLOPT_WRITEFUNCTION will write the data to the FILE * given with this option, or to stdout if this option hasn't been set. 
  218.       
  219.      If you're using libcurl as a win32 DLL, you MUST use the CURLOPT_WRITEFUNCTION if you set this option or you will experience crashes. 
  220.       
  221.      This option is also known with the older name CURLOPT_FILE, the name CURLOPT_WRITEDATA was introduced in 7.9.7. 
  222.       
  223.  
  224.      */  
  225.     //FILE *fp = fopen("/Users/kevin/Desktop/test.text", "a");  
  226.       
  227.     /*CURL *curlJson; 
  228.     curlJson = curl_easy_init(); 
  229.     curl_easy_setopt(curlJson, CURLOPT_URL, serverIP2); 
  230.     //设置curl缓存数据为2000字节 
  231.     curl_easy_setopt(curlJson, CURLOPT_BUFFERSIZE, 5000); 
  232.     curl_easy_setopt(curlJson, CURLOPT_WRITEFUNCTION, write_data); 
  233.     int a = curl_easy_perform(curlJson); 
  234.     printf("a******  %d\n",a); 
  235.     curl_easy_cleanup(curlJson); 
  236.     printf("%s\n",buffer); 
  237.     putchar(10);*/  
  238.       
  239.       
  240.     /*CURL *curl; 
  241.     CURLcode res; 
  242.     curl = curl_easy_init(); 
  243.     if (curl) 
  244.     { 
  245.         curl_easy_setopt(curl, CURLOPT_URL,serverIP2); 
  246.         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); 
  247.         res = curl_easy_perform(curl); 
  248.          
  249.     } 
  250.      
  251.     curl_easy_cleanup(curl);*/  
  252.     //7.easyHandle向服务器发送表单  
  253.     /*CURL *curl = curl_easy_init(); 
  254.     struct curl_httppost *post = NULL; 
  255.     struct curl_httppost *last = NULL; 
  256.     if (curl) { 
  257.         curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.1.112/jforum?module=shoot&action=start"); 
  258.          
  259.         curl_formadd(&post, &last,CURLFORM_COPYNAME,"name",CURLFORM_COPYCONTENTS,"kevin",CURLFORM_END); 
  260.         curl_formadd(&post, &last,CURLFORM_COPYNAME,"passsWord",CURLFORM_COPYCONTENTS,"1234",CURLFORM_END); 
  261.         curl_easy_setopt(curl, CURLOPT_HTTPPOST, post); 
  262.         curl_easy_perform(curl); 
  263.  
  264.     } 
  265.     curl_easy_cleanup(curl); 
  266.     curl_formfree(post);*/  
  267.       
  268.     //8.多线程解决请求  
  269.     /*CURL *curlJson; 
  270.     curlJson = curl_easy_init(); 
  271.     CURLM *multi; 
  272.     int running_handles; 
  273.      
  274.      
  275.      
  276.     curl_easy_setopt(curlJson, CURLOPT_URL, serverIP2); 
  277.     //curl_easy_setopt(curlJson, CURLOPT_WRITEFUNCTION,write_data); 
  278.     //curl_easy_perform(curlJson); 
  279.      
  280.     multi = curl_multi_init(); 
  281.     curl_multi_add_handle(multi, curlJson); 
  282.     if (multi) { 
  283.         curl_multi_setopt(multi, CURLOPT_WRITEFUNCTION, write_data); 
  284.         curl_multi_perform(multi, &running_handles); 
  285.     } 
  286.     curl_easy_cleanup(curlJson); 
  287.     curl_multi_cleanup(multi); 
  288.     printf("%s\n",buffer); 
  289.     putchar(10);*/  
  290.       
  291.     return 0;  
  292.       
  293.       
  294.       
  295. }  

上面的文章,有时间我会更新,实在是有点杂乱,非常不利于学习。

下面看下Curl访问网络时候,控制台打印输出的详细网络参数。

将下面代码添加就OK

[cpp]  view plain copy
  1. curl_easy_setopt(easyHandle1, CURLOPT_VERBOSE, 1L);  

现在看下连接的打印信息:


[cpp]  view plain copy
  1. //ip地址和端口号  
  2. * About to connect() to 192.168.1.5 port 80 (#0)  
  3. //开始连接  
  4. *   Trying 192.168.1.5...  
  5. * connected  
  6. * Connected to 192.168.1.5 (192.168.1.5) port 80 (#0)  
  7. //ip地址后的方法参数  
  8. > GET /jforum?module=shoot&action=load HTTP/1.1  
  9. //host address  
  10. Host: 192.168.1.5  
  11. //读取本地的cookie文件  
  12. Accept: *  
  13. Cookie: jforumUserHash=7b96d4832d66aca47a9e9d11fff1de69; jforumAutoLogin=1; jforumUserId=305; JSESSIONID=09D0FE46A767973E1A1EF89B1D771693.jvm1  
  14.   
  15. //返回状态码200 和ok  
  16. < HTTP/1.1 200 OK  
  17. * Replaced cookie JSESSIONID="7E0D21DF09402CE07494F8BE33895459.jvm1" for domain 192.168.1.5, path /, expire 0  
  18. < Set-Cookie: JSESSIONID=7E0D21DF09402CE07494F8BE33895459.jvm1; Path=/  
  19. //utf8编码,文本或者静态网页  
  20. < Content-Type: text/html;charset=UTF-8  
  21. < Transfer-Encoding: chunked  
  22. //时间戳  
  23. < Date: Sat, 10 Aug 2013 02:44:43 GMT  
  24. //服务器架构  
  25. < Server: Apache-Coyote/1.1  
  26. <  
  27. //打印返回信息  
  28. Cocos2d: Network:jsonDataLoad: {"er":0,"zs":0,"fb":2090,"un":"Player_65315","dh":13700,"infoTime":600,"id":305}  
  29. 0  
  30.   
  31.   
  32. * Connection #0 to host 192.168.1.5 left intact  
  33. //关闭连接  
  34. * Closing connection #0  




别着急这时候还会提示头文件找不到;

在xcode中点击你的cocos2dx项目,然后选择你项目的 targets,然后在Build Settings中找到 Search Paths:

双击你的 Library Search Paths 观察:如下图:

 

下面那个”$…../third_party/ios/libraries”路径是你第一步添加lib curl.a的时候默认添加的。这个我们不要修改;但是请双击这个路径然后copy下来;

我们需要修改的是此属性的上一个属性,Header Search Paths;

双击Header Search Paths属性后面的连接,然后点击“+”号添加一个路径,这个路径就是刚才你copy的路径,但是粘贴后还要将此路径设置到上一个文件夹的路径;这么说有点绕,其实就是如下:

假设你之前copy的路径是  ”$…../third_party/ios/libraries”

   那么你在这里粘贴的时候路径应该是: ”$…../third_party/ios”

OK,Himi这里的路径也截图给大家一张便于对比:

 

OK,如果以上步骤都操作正常那么编译将没有任何问题;

编译成功后,command+R运行项目,观察xcode控制台打印,以及服务器端打印:正常情况下应该如下:

 

OK,一切正常;

       注意:用脚本新建的工程,默认是不加libcurl的,大家编译到其他平台的时候要修改makefile文件将其添加进去;(具体可以参考tests里面的makefile 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值