Cocos2d-x 3.x进行HttpGet请求实例

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012997311/article/details/90107915

Cocos2d-x进行Http请求需要三个类:HttpClient,HttpRequest,HttpResponse.

1.引入头文件,引用命名空间:

#include"network\HttpClient.h"
#include"network\HttpRequest.h"
#include"network\HttpResponse.h"
using namespace cocos2d::network;

2.创建HttpRequest,设置URL,请求方式,请求标签

	auto request = new HttpRequest();
	request->setUrl("http://baidu.com/");
	request->setRequestType(HttpRequest::Type::GET);
	request->setTag("request tag");

3.设定回调函数(这里我直接用了Lambda表达式)

request->setResponseCallback([=](HttpClient* client, HttpResponse * response)
	{
		if (response->isSucceed() == true)
		{
			std::vector<char> * str = response->getResponseData();
			std::string strstr(str->begin(), str->end());
			CCLOG("%s", strstr.data());
		}

	});

4.使用HttpClient发送请求

HttpClient *client = HttpClient::getInstance();
	client->send(request);
	request->release();

5.控制台CCLOG打印结果

展开阅读全文

没有更多推荐了,返回首页