[cocos2dx]网络请求httpClient

xiaoxiao2021-02-27  596

HttpClient是HTTP客户端的接口。HttpClient封装了各种对象,处理cookies,身份认证,连接管理等。

概念

HttpClient的使用一般包含下面6个步骤:

创建 HttpRequest 的实例;设置某种连接方法的类型(GET、POST等),这里通过setUrl传入待连接的地址;设置响应回调函数,读取response;添加请求到HttpClient任务队列;释放连接。无论执行方法是否成功,都必须释放连接;对得到后的内容进行处理。

如何使用

引入头文件和命名空间

#include "network/HttpClient.h" using namespace cocos2d::network;

HttpRequest 实例

我们将使用HttpRequest无参数的构造函数,它为大多数情况提供了一个很好的默认设置,所以我们使用它。

HttpRequest* request = new HttpRequest();

设置连接方法的类型和待连接的地址

由HTTP规范定义的各种方法对应各种不同的HttpRequest类。

我们将使用Get方法,这是一个简单的方法,它只是简单地取得一个URL,获取URL指向的文档。

request->setRequestType(HttpRequest::Type::GET); request->setUrl("http://www.httpbin.org/get");

设置回调

无论服务器返回怎样的状态,响应主体response body总是可读的,这至关重要。

request->setResponseCallback(CC_CALLBACK_2(HelloWorld::onHttpComplete,this));

在onHttpComplete里读取响应数据:

std::vector<char> *buffer = response->getResponseData();//Get the request data pointer back

添加请求到HttpClient任务队列

cocos2d::network::HttpClient::getInstance()->send(request);

释放连接

这是一个可以让整个流程变得完整的关键步骤, 我们必须告诉HttpClient,我们已经完成了连接,并且它现在可以重用。如果不这样做的话,HttpClient将无限期地等待一个连接释放,以便它可以重用。

要释放连接,使用:

request->release();

处理响应

现在,我们已经完成了与HttpClient的交互,可以集中精力做我们需要处理的数据。在这个例子中,我们仅仅将它在控制台上输出。

// dump data std::vector *buffer = response->getResponseData(); printf("Http Test, dump data: "); for (unsigned int i = 0; i < buffer->size(); i++) { printf("%c", (*buffer)[i]); } printf("\n");

如果你需要把response作为一个流来读取它里面的信息,上面的步骤将会同如何解析这个连接结合,当你处理完所有的数据后,关闭输入流,并释放该连接。

GET请求示例

下面是一个通过HttpClient的HTTP GET请求的例子。

HttpRequest* request = new HttpRequest(); request->setUrl("http://www.baidu.com"); request->setRequestType(HttpRequest::Type::GET); request->setResponseCallback(CC_CALLBACK_2(HelloWorld::onHttpRequestCompleted,this)); request->setTag("GET test"); cocos2d::network::HttpClient::getInstance()->send(request); request->release();

POST请求示例

下面将发送一个POST请求到URL“http://httpbin.org/post”。

HttpRequest* request = new HttpRequest(); request->setUrl("http://httpbin.org/post"); request->setRequestType(HttpRequest::Type::POST); request->setResponseCallback(CC_CALLBACK_2(HelloWorld::onHttpRequestCompleted,this)); // write the post data const char* postData = "visitor=cocos2d&TestSuite=Extensions Test/NetworkTest"; request->setRequestData(postData, strlen(postData)); request->setTag("POST test"); cocos2d::network::HttpClient::getInstance()->send(request); request->release();

处理网络回调函数

void HelloWorld::onHttpRequestCompleted(HttpClient *sender, HttpResponse *response) { if (!response) { return; } // You can get original request type from: response->request->reqType if (0 != strlen(response->getHttpRequest()->getTag())) { log("%s completed", response->getHttpRequest()->getTag()); } int statusCode = response->getResponseCode(); char statusString[64] = {}; sprintf(statusString, "HTTP Status Code: %d, tag = %s", statusCode, response->getHttpRequest()->getTag()); _labelStatusCode->setString(statusString); log("response code: %d", statusCode); if (!response->isSucceed()) { log("response failed"); log("error buffer: %s", response->getErrorBuffer()); return; } // dump data std::vector<char> *buffer = response->getResponseData(); printf("Http Test, dump data: "); for (unsigned int i = 0; i < buffer->size(); i++) { printf("%c", (*buffer)[i]); } printf("\n"); }

Android

需要注意的是,如果你是Android环境,不要忘了在您的应用程序的Manifest 中增加相应的权限:

<uses-permission android:name=”android.permission.INTERNET” />

详细代码可参照..\tests\Cpp\TestCpp\Classes\ExtensionsTest\NetworkTest\HttpClientTest.cpp

HttpManager架构

HttpManager.h文件

#pragma once #include "cocos2d.h" #include "network/HttpClient.h" //#include "Core/FV/FvSingleton.h" USING_NS_CC; using namespace network; using namespace std; class HttpManagerDelegate { public: virtual void onHttpManagerRequestCompleted(HttpClient* sender,HttpResponse* response) = 0; }; class HttpManager //: public FvSingleton<HttpManager> { public: HttpManager(void); ~HttpManager(void); CC_SYNTHESIZE(HttpManagerDelegate*,_httpManagerDelegate,HttpDelegate); //等价于 HttpManagerDelegate* getHttpDelegate(){return _httpManagerDelegate;}; //等价于 void setHttpDelegate(HttpManagerDelegate* delegate){_httpManagerDelegate = delegate;}; //static HttpManager* getInstance(); void sendGetRequest(string url,string requestTag); void sendPostRequest(string url,string requestTag); private: void onHttpRequestCompleted(HttpClient* sender,HttpResponse* response); //static HttpManager* mHttpManager; };

HttpManager.cpp文件

#include "HttpManager.h" //FV_SINGLETON_STORAGE(HttpManager); //HttpManager* HttpManager::mHttpManager = nullptr; HttpManager::HttpManager(void) { _httpManagerDelegate = nullptr; HttpClient::getInstance()->setTimeoutForConnect(5); } HttpManager::~HttpManager(void) { } HttpManager* HttpManager::getInstance() { if (mHttpManager == nullptr) { mHttpManager = new HttpManager(); } return mHttpManager; } void HttpManager::sendGetRequest(string url,string requestTag) { HttpRequest* request = new (std::nothrow) HttpRequest(); request->setRequestType(HttpRequest::Type::GET); request->setUrl(url.c_str()); request->setResponseCallback(CC_CALLBACK_2(HttpManager::onHttpRequestCompleted,this)); request->setTag(requestTag.c_str()); HttpClient::getInstance()->send(request); request->release(); } void HttpManager::sendPostRequest(string url,string requestTag) { HttpRequest* request = new (std::nothrow) HttpRequest(); request->setRequestType(HttpRequest::Type::POST); request->setUrl(url.c_str()); request->setResponseCallback(CC_CALLBACK_2(HttpManager::onHttpRequestCompleted,this)); request->setTag(requestTag.c_str()); request->setRequestData("",0); HttpClient::getInstance()->send(request); request->release(); } void HttpManager::onHttpRequestCompleted(HttpClient* sender,HttpResponse* response) { if (!response) { return; } const char* tag = response->getHttpRequest()->getTag(); if (0 != strlen(tag)) { log("%s completed",tag); } log("HTTP Status Code::%ld, tag = %s",response->getResponseCode(),tag); //if (!response->isSucceed()) //{ // log("response failed"); // log("error buffer:%s",response->getErrorBuffer()); // mMapCallBack.erase(tag); // return; //} if (_httpManagerDelegate) { _httpManagerDelegate->onHttpManagerRequestCompleted(sender,response); } }

BLLHttpTest.h文件

#pragma once #include "Core/FV/FvSingleton.h" #include "Core/Network/HttpManager.h" using namespace std; class BLLHttpTest : public HttpManagerDelegate//,public FvSingleton<BLLHttpTest> { public: BLLHttpTest(void); ~BLLHttpTest(void); static BLLHttpTest* getInstance(); typedef std::function<void(string responseData,const char* tag)> onResponseCallBack; void sendGetRequest(string url,string requestTag,onResponseCallBack cb); void sendPostRequest(string url,string requestTag,onResponseCallBack cb); virtual void onHttpManagerRequestCompleted(HttpClient* sender,HttpResponse* response); private: void onHttpRequestCompletedDataHandle(string data,string tag); std::map<string, onResponseCallBack> mMapCallBack; static BLLHttpTest* mBLLHttpTest; HttpManager* mHttpManager; };

 

BLLHttpTest.cpp文件

#include "BLLHttpTest.h" //FV_SINGLETON_STORAGE(BLLHttpTest); BLLHttpTest* BLLHttpTest::mBLLHttpTest = nullptr; BLLHttpTest::BLLHttpTest(void) { mHttpManager = new HttpManager(); mHttpManager->setHttpDelegate(this); } BLLHttpTest::~BLLHttpTest(void) { } BLLHttpTest* BLLHttpTest::getInstance() { if (!mBLLHttpTest) { mBLLHttpTest = new BLLHttpTest(); } return mBLLHttpTest; } void BLLHttpTest::sendGetRequest(string url,string requestTag,onResponseCallBack cb) { mMapCallBack[requestTag] = cb; mHttpManager->sendGetRequest(url,requestTag); } void BLLHttpTest::sendPostRequest(string url,string requestTag,onResponseCallBack cb) { mMapCallBack[requestTag] = cb; mHttpManager->sendPostRequest(url,requestTag); } void BLLHttpTest::onHttpManagerRequestCompleted(HttpClient* sender,HttpResponse* response) { const char* tag = response->getHttpRequest()->getTag(); if (!response->isSucceed()) { log("response failed"); log("error buffer:%s",response->getErrorBuffer()); mMapCallBack.erase(tag); return; } std::vector<char> *buffer = response->getResponseData(); string temp = ""; for (unsigned int i = 0; i < buffer->size(); i++) { temp +=(*buffer)[i]; } log("Http Test, dump data:%s\n",temp.c_str()); if (mMapCallBack[tag]) { onHttpRequestCompletedDataHandle(temp,tag); mMapCallBack[tag](temp,tag); mMapCallBack.erase(tag); } } void BLLHttpTest::onHttpRequestCompletedDataHandle(string data,string tag) { }

几乎所有的架构中,涉及到回调函数,都会把回调函数封装成一个纯虚类,子类继承这个纯虚类并实现纯虚函数。然后在子类的构造函数中,将自己给设置成代理这HttpManager架构运用了Bridge桥接设计模式

转载请注明原文地址: https://www.6miu.com/read-136.html

最新回复(0)