加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2d-x 网络编程Curl

发布时间:2020-12-14 19:03:32 所属栏目:百科 来源:网络整理
导读:1 CURLcode curl_global_init(long flags); 描述: 这个函数只能用一次。(其实在调用curl_global_cleanup 函数后仍然可再用) 如果这个函数在curl_easy_init函数调用时还没调用,它讲由libcurl库自动完成。 参数:flags CURL_GLOBAL_ALL //初始化所有的可能的
1 CURLcode curl_global_init(long flags);

描述:
这个函数只能用一次。(其实在调用curl_global_cleanup 函数后仍然可再用)
如果这个函数在curl_easy_init函数调用时还没调用,它讲由libcurl库自动完成。
参数:flags
CURL_GLOBAL_ALL //初始化所有的可能的调用。
CURL_GLOBAL_SSL //初始化支持 安全套接字层。
CURL_GLOBAL_WIN32 //初始化win32套接字库。
CURL_GLOBAL_NOTHING //没有额外的初始化。
2 void curl_global_cleanup(void);
描述:在结束libcurl使用的时候,用来对curl_global_init做的工作清理。类似于close的函数。
3 char *curl_version( );
描述: 打印当前libcurl库的版本。
4 CURL *curl_easy_init( );
描述:
curl_easy_init用来初始化一个CURL的指针(有些像返回FILE类型的指针一样). 相应的在调用结束时要用curl_easy_cleanup函数清理.
一般curl_easy_init意味着一个会话的开始. 它的返回值一般都用在easy系列的函数中.
5 void curl_easy_cleanup(CURL *handle);
这个调用用来结束一个会话.与curl_easy_init配合着用.
参数:
CURL类型的指针.
6 CURLcode curl_easy_setopt(CURL *handle,CURLoption option,parameter);
描述: 这个函数最重要了.几乎所有的curl 程序都要频繁的使用它.
它告诉curl库.程序将有如何的行为. 比如要查看一个网页的html代码等.
(这个函数有些像ioctl函数)
1 CURL类型的指针
2 各种CURLoption类型的选项.(都在curl.h库里有定义,man 也可以查看到)
3 parameter 这个参数 既可以是个函数的指针,也可以是某个对象的指针,也可以是个long型的变量.它用什么这取决于第二个参数.
CURLoption 这个参数的取值很多.具体的可以查看man手册.
7 CURLcode curl_easy_perform(CURL *handle);
描述:这个函数在初始化CURL类型的指针 以及curl_easy_setopt完成后调用. 就像字面的意思所说perform就像是个舞台.让我们设置的
option 运作起来.

CURL类型的指针


例子:

  1. staticboolisLogin;
  2. CurlTest::CurlTest()
  3. {
  4. CCLabelTTF*label=CCLabelTTF::create("CurlTest","Arial",28);
  5. addChild(label,0);
  6. label->setPosition(ccp(VisibleRect::center().x,VisibleRect::top().y-50));
  7. setTouchEnabled(true);
  8. //createalabeltodisplaythetipstring
  9. m_pLabel=CCLabelTTF::create("Touchthescreentoconnect",22);
  10. m_pLabel->setPosition(VisibleRect::center());
  11. addChild(m_pLabel,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">
  12. m_pLabel->retain();
  13. isLogin=false;
  14. }
  15. //thetestcodeis
  16. //http://curl.haxx.se/mail/lib-2009-12/0071.html
  17. voidCurlTest::ccTouchesEnded(CCSet*pTouches,CCEvent*pEvent)
  18. CURL*curl;
  19. CURLcoderes;
  20. charbuffer[10];
  21. stringstrHtml;
  22. stringstrRetData="";
  23. curl=curl_easy_init();
  24. if(curl)
  25. {
  26. curl_easy_setopt(curl,CURLOPT_URL,"http://localhost/mobTest.php?user=cistudio&password=123");
  27. curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,HttpWriteString);
  28. //对认证证书来源的检查,0表示阻止对证书的合法性的检查。
  29. //从证书中检查SSL加密算法是否存在
  30. //模拟用户使用的浏览器,在HTTP请求中包含一个”user-agent”头的字符串。
  31. "Mozilla/4.0(compatible;MSIE8.0;WindowsNT5.0)");
  32. //curl_easy_setopt(curl,CURLOPT_COOKIEFILE,"cookiefile.txt");
  33. //将登录信息记录并生成到一个cookies文件中
  34. "cookiefile.txt");
  35. //获取的信息以文件流的形式返回,而不是直接输出。
  36. res=curl_easy_perform(curl);
  37. /*alwayscleanup*/
  38. curl_easy_cleanup(curl);
  39. if(res==CURLE_OK)
  40. m_pLabel->setString("0response");
  41. strRetData=strHtml;
  42. CCLOG("Httpgetstring,ret:%s",strRetData.c_str());
  43. }
  44. else
  45. sprintf(buffer,"code:%i",res);
  46. m_pLabel->setString(buffer);
  47. m_pLabel->setString("nocurl");
  48. size_tCurlTest::HttpWriteString(uint8_t*ptr,size_tsize,87); font-weight:bold; background-color:inherit">size_tnumber,void*stream)
  49. chartmpStr[10];
  50. sprintf(tmpStr,"%s",ptr);
  51. if(tmpStr=="OK"){
  52. true;
  53. }else{
  54. isLogin=false;
  55. CURL*curl;
  56. CURLcoderes;
  57. "http://localhost/isLoginTest.php");
  58. true);
  59. //对认证证书来源的检查,0表示阻止对证书的合法性的检查。
  60. //从证书中检查SSL加密算法是否存在
  61. //模拟用户使用的浏览器,在HTTP请求中包含一个”user-agent”头的字符串。
  62. "Mozilla/4.0(compatible;MSIE8.0;WindowsNT5.0)");
  63. //读取cookies中的信息供给服务器调用
  64. "cookiefile.txt");
  65. CCLog("completesgetLoginState");
  66. CCLog("%s%1d",ptr,number);
  67. returnsize*number;//这里一定要返回实际返回的字节数
  68. size_tCurlTest::getLoginState(uint8_t*ptr,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> CCLog("%s",0); background-color:inherit">//这里一定要返回实际返回的字节数
  69. CurlTest::~CurlTest()
  70. m_pLabel->release();
  71. voidCurlTestScene::runThisTest()
  72. CCLayer*pLayer=newCurlTest();
  73. addChild(pLayer);
  74. CCDirector::sharedDirector()->replaceScene(this);
  75. pLayer->release();
  76. }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读