cocos2dx 网络编程(CCHttpRequest和CURL两个方式)

转自:http://blog.csdn.net/sg619262284/article/details/20144087

在使用之前需要设置一些参数:参考:http://blog.csdn.net/wangbin_jxust/article/details/9632771

在完成上面的操作后,还需要在链接器的输入里面添加一个参数pthreadVCE2.lib;


CURL提供了阻塞传输(curl_easy_)和(非阻塞传输)curl_mutl_两种方式。CCHttpClient对CURL进行封装,采用http的方式传输数据。

使用CCHttpRequest方法实现:(异步连接)

  1. void HallView::Qudian(){//网络异步连接方法  
  2.         HttpRequest* request = new HttpRequest();  
  3.     request->setRequestType(HttpRequest::Type::POST);  
  4.     request->setUrl("www.baidu.com"));  
  5.     request->setTag(tag);  
  6.     request->setResponseCallback(this,onHttpRequestCompleted);     
  7.     request->setRequestData(date, strlen(date));  
  8.   
  9.     HttpClient* httpClient=HttpClient::getInstance();  
  10.     httpClient->setTimeoutForConnect(time);  
  11.     httpClient->setTimeoutForRead(time);  
  12.     httpClient->send(request);  
  13.     request->release();  
  14. }  

RequestType共有五种模式:kHttpGet、kHttpPost、kHttpPut、kHttpDelete、kHttpUnkown。kHttpUnkown是默认的请求模式


添加一个回调方法。

  1. void HallView::onHttpRequestCompleted(cocos2d::CCNode *sender ,void *data){  
  2.     cocos2d::extension::CCHttpResponse* response=(cocos2d::extension::CCHttpResponse*)data;  
  3.     if(!response) {CCLOG("Log:response =null,plase check it."); return;}  
  4.       
  5.      //请求失败  
  6.     if(!response->isSucceed())  
  7.     {  
  8.         this->removeChildByTag(Animate_loading,true);  
  9.         CCDictionary* pDict = CCDictionary::createWithContentsOfFile("chines.xml");  
  10.         platform::showMsg(((CCString*)pDict->objectForKey("networking"))->getCString());  
  11.         CCLOG("ERROR BUFFER:%s",response->getErrorBuffer());  
  12.         return;  
  13.     }     
  14.   
  15.     int codeIndex=response->getResponseCode();  
  16.     const char* tag=response->getHttpRequest()->getTag();  
  17.       
  18.     //请求成功  
  19.     std::vector<char>* buffer=response->getResponseData();  
  20.     std::string temp(buffer->begin(),buffer->end());  
  21.     CCString* responseData=CCString::create(temp);  
  22.     Json::Reader reader;//json解析    
  23.     Json::Value value;//表示一个json格式的对象    
  24.     if(reader.parse(responseData->getCString(),value))//解析出json放到json中区    
  25.     {    
  26.           //这里就可以对返回来的信息做处理  
  27.         }  
  28.           
  29. }  



使用异步连接,程序和联网的方法将互相不干扰,联网方法将为一个独立的线程。


使用CURL方法实现:阻塞式连接

需要加入 头文件#include "curl/curl.h"

  1. void HallView::denglu(){    //登陆游戏  
  2.     CURL *curl;    
  3.     CURLcode res;    
  4.     string cc;   
  5.     curl=curl_easy_init();    
  6.     if(curl)    
  7.     {   
  8.         curl_easy_setopt(curl, CURLOPT_URL, ""); //设置请求的地址    
  9.         curl_easy_setopt(curl, CURLOPT_POST, true); //设置数据类型  
  10.         string caozuo="";  
  11.         curl_easy_setopt(curl, CURLOPT_POSTFIELDS,caozuo.c_str()); //将操作代码,和连接的网站组合,一起发送!   
  12.         curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);  
  13.         curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,HallView::writehtml); //数据处理回调函数    
  14.         curl_easy_setopt(curl, CURLOPT_WRITEDATA, &cc);//缓冲的内存    
  15.         curl_easy_setopt(curl,CURLOPT_TIMEOUT_MS,5000); //设置连接超时时间    
  16.         res=curl_easy_perform(curl);    
  17.         if(res!=CURLE_OK)    
  18.         {    
  19.             CCDictionary* pDict = CCDictionary::createWithContentsOfFile("chines.xml");  
  20.             string mes=((CCString*)pDict->objectForKey("networking"))->getCString();  
  21.             platform::showMsg(mes);  
  22.         }    
  23.         curl_easy_cleanup(curl);    
  24.     }    
  25.     else    
  26.     {    
  27.         CCLog("curl is null");    
  28.     }    
  29. }  


在定义回调函数:这个方法为静态方法,如果里面要引用其他变量,需要为静态变量。
  1. size_t HallView::writehtml(uint8_t* ptr,size_t size,size_t number,void *stream)      
  2. {       
  3.     CCString* a=CCString::createWithFormat("%s",ptr);    
  4.     std::string str1=a->getCString();  
  5.     Json::Reader reader;//json解析    
  6.     Json::Value value;//表示一个json格式的对象    
  7.     if(reader.parse(str1,value))//解析出json放到json中区    
  8.     {    
  9.         string out=value["gameId"].asString();      
  10.         gameda->gameId=out;  
  11.         out=value["newIMSI"].asString();  
  12.         gameda->newIMSI=out;  
  13.     }    
  14.     return size*number;//这里一定要返回实际返回的字节数      
  15. }  


在.h中定义:

  1. static size_t writehtml(uint8_t* ptr,size_t size,size_t number,void *stream);   



curl_easy_setopt::属性
curlopt_url//URL地址值
curlopt_writefunction//将得到的数据传递相应的函数
curlopt_writeddata//将函数传递给相应的第四个参数里
curlopt_header//如果设置为1,可以返回http头的值;如果设置为非0值,则可以把一个头包含在输出中
CURLOPT_TIMEOUT_MS //设置cURL允许执行的最长毫秒数。
curlopt_low_speed_limit//设置一个长整型。控制传送多少字节
curlopt_cookie//传递一个包含httpcookie的头连接
curlopt_flie//传送到输出文件
curlopt_infile//传送过来的输出文件
curlopt_writeheader//输出头部分
curlopt_proxyuserpwd//传递一个形如[username]:[password]格式的字符串去连接http代理
curlopt_postfields//传递一个作为httppost操作的所有数据的字符串
curlopt_referer //在http请求中包含一个referer头的字符串
curlopt_useragent//在http请求中包含一个user-agent 头的字符串
curlpot_ftpport 传递一个包含被ftppost指令使用的IP地址
使用格式curl_easy_setopt( curl, CURLOPT_FOLLOWLOCATION, 1L); //第一个参数实例化的curl,第二个数属性,第三个为属性值


如果,获取的返回值是josn格式,我的博客中有方法非常方便提取指定的值。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值