【唠叨】
HTTP用于客户端终端(用户)与服务器(WEB)之间的数据通讯。
若菜不懂WEB,也不会服务端开发。所以在这里只能讲讲客户端如果使用http连接WEB服务器了。
常见的WEB服务器有PHP、JSP、ASP等。
Cocos为我们封装了HTTP客户端的接口:HttpClient类。HttpClient封装了各种对象,处理cookies,身份认证,连接管理等。
【参考】
http://www.jb51.cc/article/p-ndohzuqc-ep.html(弱联网与服务器的通讯)
【Demo下载】
https://github.com/shahdza/Cocos_LearningTest/tree/master/HttpClient
【HTTP简介】
Wiki百科 :http://zh.m.wikipedia.org/wiki/Http
超文本传输协议(HyperText Transfer Protocol,缩写:HTTP),是互联网上应用最为广泛的一种网络协议。设计HTTP最初的目的是为了提供一种发布和接收HTML页面的方法。通过HTTP或者HTTPS协议请求的资源由统一资源标识符(Uniform Resource Identifiers,URI)来标识。
HTTP是一个无连接,无状态连接的应用层协议,由请求 ― 响应构成,是标准的C/S模型,主要解决如何包装数据。
HTTP连接使用的是“请求――响应”的方式,不仅在请求时需要先建立连接,而且需要客户端向服务器发出请求后,服务器端才能回复数据。
HTTP主要由两种请求方式:GET、POST。
>get是从服务器上获取(查询)数据;而post是向服务器传送(修改)数据。
>get传输数据量较小,2KB;而post传输一般不受限制。
>get安全性非常低,但执行效率比较好;而post安全性相对来说就比较好。
建议:在做数据查询时用get方式,而在做数据添加,修改或删除时用post方式。
【HTTP连接的步骤】
(1)客户端请求服务器连接。
(2)客户端发送GET或POST指令,请求数据。
(3)服务端接收到请求数据指令后,发送响应请求的数据。
(4)释放连接,一次连接结束。
【HttpClient】
Cocos为我们封装了HTTP客户端的接口:HttpClient类。
HttpClient类是一个单例类,用于管理HTTP连接请求。
HttpClient封装了各种对象,处理cookies,身份认证,连接管理等。
还有两个重要的类:
> HttpRequest :提供发送HTTP请求有关的数据信息(包含:url、请求方式、数据、响应函数等信息)
> HttpResponse :服务器返回的响应信息(包含:响应代码、错误信息、响应的数据等信息)
0、引入头文件
- //
- #include"network/HttpClient.h"
- usingnamespacenetwork;
- //
1、相关函数
- //
- /**
- * HttpRequest相关
- **/
- HttpRequest
- {
- //请求方式
- HttpRequest::Type::GET
- HttpRequest::Type::POST
- //设置请求方式
- voidsetRequestType(Typetype);
- //设置访问服务器的资源URL
- voidsetUrl(constchar*url);
- //发送请求数据,用于POST方式
- voidsetRequestData(constchar*buffer,size_tlen);
- //绑定请求的响应函数
- voidsetResponseCallback(constccHttpRequestCallback&callback);
- //HttpResponse为服务器返回的响应信息
- callback(HttpClient*sender,HttpResponse*response);
- };
- /**
- * HttpResponse相关
- **/
- HttpResponse
- {
- //HTTP请求是否成功
- boolisSucceed();
- //响应代码
- longgetResponseCode();
- //错误信息
- constchar*getErrorBuffer();
- //发送回来的响应数据信息
- std::vector<char>*getResponseData();
- };
- /**
- * HttpClient相关
- **/
- HttpClient
- {
- //获取HttpClient单例
- staticHttpClient*getInstance();
- staticvoiddestroyInstance();
- //发送HTTP请求
- voidsend(HttpRequest*request);
- //设置连接超时时长
- inlinevoidsetTimeoutForConnect(intvalue){_timeoutForConnect=value;};
- //设置下载超时时长
- inlinevoidsetTimeoutForRead(intvalue){_timeoutForRead=value;};
- };
- //
2、使用步骤
(1)创建HttpRequest请求
(2)设置request请求的url、请求方式GET/POST、发送的数据、请求的响应函数。
(3)发送request请求,HttpClient::getInstance()->send(request); 。
(4)处理请求的响应函数。
(5)释放HTTP连接。
3、代码例子
- //
- //发送HTTP请求
- voidHelloWorld::onHttpRequest(std::stringtype)
- {
- //创建HTTP请求
- HttpRequest*request=newHttpRequest();
- if(type=="get")
- {
- request->setRequestType(HttpRequest::Type::GET);
- //url后面附加数据信息
- request->setUrl("http://httpbin.org/get?name=hello&pwd=world");
- }
- elseif(type=="post")
- {
- request->setRequestType(HttpRequest::Type::POST);
- request->setUrl("http://httpbin.org/post");
- //设置post发送请求的数据信息
- std::stringdata="helloworld!";
- request->setRequestData(data.c_str(),data.length());
- }
- //HTTP响应函数
- request->setResponseCallback(CC_CALLBACK_2(HelloWorld::onHttpResponse,this));
- //发送请求
- HttpClient::getInstance()->send(request);
- //释放链接
- request->release();
- }
- //HTTP响应请求函数
- voidHelloWorld::onHttpResponse(HttpClient*sender,HttpResponse*response)
- {
- //没有收到响应
- if(!response)
- {
- CCLOG("noresponse");
- return;
- }
- intstatusCode=response->getResponseCode();
- charstatusString[64]={};
- sprintf(statusString,"HTTPStatusCode:%d,tag=%s",statusCode,response->getHttpRequest()->getTag());
- CCLOG("responsecode:%s",statusString);
- //链接失败
- if(!response->isSucceed())
- {
- CCLOG("responseFailed");
- CCLOG("errorbuffer:%s",response->getErrorBuffer());
- return;
- }
- //获取数据
- std::vector<char>*v=response->getResponseData();
- for(inti=0;i<v->size();i++)
- {
- printf("%c",v->at(i));
- }
- printf("\n");
- }
- //
4、运行结果