转自:http://blog.csdn.net/sg619262284/article/details/20144087
在使用之前需要设置一些参数:参考:http://www.52php.cn/article/p-ydwlaxpd-tm.html
在完成上面的操作后,还需要在链接器的输入里面添加一个参数pthreadVCE2.lib;
CURL提供了阻塞传输(curl_easy_)和(非阻塞传输)curl_mutl_两种方式。CCHttpClient对CURL进行封装,采用http的方式传输数据。
使用CCHttpRequest方法实现:(异步连接)
[cpp]
view plain
copy
print
?
- voidHallView::Qudian(){
- HttpRequest*request=newHttpRequest();
- request->setRequestType(HttpRequest::Type::POST);
- request->setUrl("www.baidu.com"));
- request->setTag(tag);
- request->setResponseCallback(this,onHttpRequestCompleted);
- request->setRequestData(date,strlen(date));
- HttpClient*httpClient=HttpClient::getInstance();
- httpClient->setTimeoutForConnect(time);
- httpClient->setTimeoutForRead(time);
- httpClient->send(request);
- request->release();
- }
RequestType共有五种模式:kHttpGet、kHttpPost、kHttpPut、kHttpDelete、kHttpUnkown。kHttpUnkown是默认的请求模式
添加一个回调方法。
[cpp]
view plain
copy
print
?
- voidHallView::onHttpRequestCompleted(cocos2d::CCNode*sender,void*data){
- cocos2d::extension::CCHttpResponse*response=(cocos2d::extension::CCHttpResponse*)data;
- if(!response){CCLOG("Log:response=null,plasecheckit.");return;}
- if(!response->isSucceed())
- {
- this->removeChildByTag(Animate_loading,true);
- CCDictionary*pDict=CCDictionary::createWithContentsOfFile("chines.xml");
- platform::showMsg(((CCString*)pDict->objectForKey("networking"))->getCString());
- CCLOG("ERRORBUFFER:%s",response->getErrorBuffer());
- return;
- }
- intcodeIndex=response->getResponseCode();
- constchar*tag=response->getHttpRequest()->getTag();
- std::vector<char>*buffer=response->getResponseData();
- std::stringtemp(buffer->begin(),buffer->end());
- CCString*responseData=CCString::create(temp);
- Json::Readerreader;
- Json::Valuevalue;
- if(reader.parse(responseData->getCString(),value))
- {
- }
- }
使用异步连接,程序和联网的方法将互相不干扰,联网方法将为一个独立的线程。
使用CURL方法实现:阻塞式连接
需要加入 头文件#include "curl/curl.h"
[cpp]
view plain
copy
print
?
- voidHallView::denglu(){
- CURL*curl;
- CURLcoderes;
- stringcc;
- curl=curl_easy_init();
- if(curl)
- {
- curl_easy_setopt(curl,CURLOPT_URL,"");
- curl_easy_setopt(curl,CURLOPT_POST,true);
- stringcaozuo="";
- curl_easy_setopt(curl,CURLOPT_POSTFIELDS,caozuo.c_str());
- curl_easy_setopt(curl,CURLOPT_FOLLOWLOCATION,1L);
- curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,HallView::writehtml);
- curl_easy_setopt(curl,CURLOPT_WRITEDATA,&cc);
- curl_easy_setopt(curl,CURLOPT_TIMEOUT_MS,5000);
- res=curl_easy_perform(curl);
- if(res!=CURLE_OK)
- {
- CCDictionary*pDict=CCDictionary::createWithContentsOfFile("chines.xml");
- stringmes=((CCString*)pDict->objectForKey("networking"))->getCString();
- platform::showMsg(mes);
- }
- curl_easy_cleanup(curl);
- }
- else
- {
- CCLog("curlisnull");
- }
- }
在定义回调函数:这个方法为静态方法,如果里面要引用其他变量,需要为静态变量。
[cpp]
view plain
copy
print
?
- size_tHallView::writehtml(uint8_t*ptr,size_tsize,size_tnumber,void*stream)
- {
- CCString*a=CCString::createWithFormat("%s",ptr);
- std::stringstr1=a->getCString();
- Json::Readerreader;
- Json::Valuevalue;
- if(reader.parse(str1,value))
- {
- stringout=value["gameId"].asString();
- gameda->gameId=out;
- out=value["newIMSI"].asString();
- gameda->newIMSI=out;
- }
- returnsize*number;
- }
在.h中定义:
[cpp]
view plain
copy
print
?
- staticsize_twritehtml(uint8_t*ptr,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,1L); //第一个参数实例化的curl,第二个数属性,第三个为属性值
如果,获取的返回值是josn格式,我的博客中有方法非常方便提取指定的值。 (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|