Cocos网络篇[3.2](2) ――HTTP连接

前端之家收集整理的这篇文章主要介绍了Cocos网络篇[3.2](2) ――HTTP连接前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

【唠叨】

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 IdentifiersURI)来标识。

HTTP是一个无连接,无状态连接的应用层协议,由请求 ― 响应构成,是标准的C/S模型,主要解决如何包装数据。

HTTP连接使用的是“请求――响应”的方式,不仅在请求时需要先建立连接,而且需要客户端向服务器发出请求后,服务器端才能回复数据。

HTTP主要由两种请求方式:GET、POST

  1. //
  2. //用GET获取表单数据,表单数据对任何人都是可见的
  3. http://www.abc.com/index.PHP?username=shahdza&password=123
  4.  
  5. //用POST获取表单数据,表单数据则是不可见的
  6. http://www.abc.com.cn/index.PHP
  7. //

>get是从服务器上获取查询)数据;而post是向服务器传送(修改)数据。

>get传输数据量较小,2KB;而post传输一般不受限制。

>get安全性非常低,但执行效率比较好;而post安全性相对来说就比较好。

建议:在做数据查询时用get方式,而在做数据添加修改删除时用post方式。


【HTTP连接的步骤】

(1)客户端请求服务器连接。

(2)客户端发送GET或POST指令,请求数据。

(3)服务端接收到请求数据指令后,发送响应请求的数据。

(4)释放连接,一次连接结束。

wKioL1T3FxKQ_AnvAAB8jvhVr2w997.jpg




【HttpClient】

Cocos为我们封装了HTTP客户端的接口:HttpClient类

HttpClient类是一个单例类,用于管理HTTP连接请求。

HttpClient封装了各种对象,处理cookies,身份认证,连接管理等。

还有两个重要的类:

> HttpRequest :提供发送HTTP请求有关的数据信息(包含:url、请求方式、数据、响应函数等信息)

> HttpResponse :服务器返回的响应信息(包含:响应代码错误信息、响应的数据等信息)


0、引入头文件

  1. //
  2. #include"network/HttpClient.h"
  3. usingnamespacenetwork;
  4. //


1、相关函数

  1. //
  2. /**
  3. * HttpRequest相关
  4. **/
  5. HttpRequest
  6. {
  7. //请求方式
  8. HttpRequest::Type::GET
  9. HttpRequest::Type::POST
  10.  
  11. //设置请求方式
  12. voidsetRequestType(Typetype);
  13.  
  14. //设置访问服务器的资源URL
  15. voidsetUrl(constchar*url);
  16.  
  17. //发送请求数据,用于POST方式
  18. voidsetRequestData(constchar*buffer,size_tlen);
  19.  
  20. //绑定请求的响应函数
  21. voidsetResponseCallback(constccHttpRequestCallback&callback);
  22.  
  23. //HttpResponse为服务器返回的响应信息
  24. callback(HttpClient*sender,HttpResponse*response);
  25. };
  26.  
  27. /**
  28. * HttpResponse相关
  29. **/
  30. HttpResponse
  31. {
  32. //HTTP请求是否成功
  33. boolisSucceed();
  34.  
  35. //响应代码
  36. longgetResponseCode();
  37.  
  38. //错误信息
  39. constchar*getErrorBuffer();
  40.  
  41. //发送回来的响应数据信息
  42. std::vector<char>*getResponseData();
  43. };
  44.  
  45. /**
  46. * HttpClient相关
  47. **/
  48. HttpClient
  49. {
  50. //获取HttpClient单例
  51. staticHttpClient*getInstance();
  52. staticvoiddestroyInstance();
  53.  
  54. //发送HTTP请求
  55. voidsend(HttpRequest*request);
  56.  
  57. //设置连接超时时长
  58. inlinevoidsetTimeoutForConnect(intvalue){_timeoutForConnect=value;};
  59.  
  60. //设置下载超时时长
  61. inlinevoidsetTimeoutForRead(intvalue){_timeoutForRead=value;};
  62. };
  63. //


2、使用步骤

(1)创建HttpRequest请求

(2)设置request请求的url、请求方式GET/POST、发送的数据、请求的响应函数

(3)发送request请求,HttpClient::getInstance()->send(request); 。

(4)处理请求的响应函数

(5)释放HTTP连接。


3、代码例子

  1. //
  2. //发送HTTP请求
  3. voidHelloWorld::onHttpRequest(std::stringtype)
  4. {
  5. //创建HTTP请求
  6. HttpRequest*request=newHttpRequest();
  7.  
  8. if(type=="get")
  9. {
  10. request->setRequestType(HttpRequest::Type::GET);
  11. //url后面附加数据信息
  12. request->setUrl("http://httpbin.org/get?name=hello&pwd=world");
  13. }
  14. elseif(type=="post")
  15. {
  16. request->setRequestType(HttpRequest::Type::POST);
  17. request->setUrl("http://httpbin.org/post");
  18. //设置post发送请求的数据信息
  19. std::stringdata="helloworld!";
  20. request->setRequestData(data.c_str(),data.length());
  21. }
  22.  
  23. //HTTP响应函数
  24. request->setResponseCallback(CC_CALLBACK_2(HelloWorld::onHttpResponse,this));
  25.  
  26. //发送请求
  27. HttpClient::getInstance()->send(request);
  28.  
  29. //释放链接
  30. request->release();
  31. }
  32.  
  33.  
  34. //HTTP响应请求函数
  35. voidHelloWorld::onHttpResponse(HttpClient*sender,HttpResponse*response)
  36. {
  37. //没有收到响应
  38. if(!response)
  39. {
  40. CCLOG("noresponse");
  41. return;
  42. }
  43.  
  44. intstatusCode=response->getResponseCode();
  45. charstatusString[64]={};
  46. sprintf(statusString,"HTTPStatusCode:%d,tag=%s",statusCode,response->getHttpRequest()->getTag());
  47. CCLOG("responsecode:%s",statusString);
  48.  
  49. //链接失败
  50. if(!response->isSucceed())
  51. {
  52. CCLOG("responseFailed");
  53. CCLOG("errorbuffer:%s",response->getErrorBuffer());
  54. return;
  55. }
  56.  
  57. //获取数据
  58. std::vector<char>*v=response->getResponseData();
  59. for(inti=0;i<v->size();i++)
  60. {
  61. printf("%c",v->at(i));
  62. }
  63. printf("\n");
  64. }
  65. //


4、运行结果

wKioL1T3IRqBf2epAAIXvKbMM-w601.jpg

猜你在找的Cocos2d-x相关文章