2012-08-09 10:45:11 +08:00
|
|
|
#include "HttpClientTest.h"
|
|
|
|
#include "../ExtensionsTest.h"
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
USING_NS_CC;
|
|
|
|
USING_NS_CC_EXT;
|
|
|
|
|
|
|
|
HttpClientTest::HttpClientTest()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _labelStatusCode(NULL)
|
2012-08-09 10:45:11 +08:00
|
|
|
{
|
|
|
|
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
|
2013-04-22 00:23:27 +08:00
|
|
|
|
|
|
|
const int MARGIN = 40;
|
|
|
|
const int SPACE = 35;
|
2012-08-09 10:45:11 +08:00
|
|
|
|
|
|
|
CCLabelTTF *label = CCLabelTTF::create("Http Request Test", "Arial", 28);
|
2013-04-22 00:23:27 +08:00
|
|
|
label->setPosition(ccp(winSize.width / 2, winSize.height - MARGIN));
|
2012-08-09 10:45:11 +08:00
|
|
|
addChild(label, 0);
|
|
|
|
|
|
|
|
CCMenu *menuRequest = CCMenu::create();
|
|
|
|
menuRequest->setPosition(CCPointZero);
|
|
|
|
addChild(menuRequest);
|
|
|
|
|
|
|
|
// Get
|
|
|
|
CCLabelTTF *labelGet = CCLabelTTF::create("Test Get", "Arial", 22);
|
2013-06-14 11:36:43 +08:00
|
|
|
CCMenuItemLabel *itemGet = CCMenuItemLabel::create(labelGet, CC_CALLBACK_1(HttpClientTest::onMenuGetTestClicked, this));
|
2013-04-22 00:23:27 +08:00
|
|
|
itemGet->setPosition(ccp(winSize.width / 2, winSize.height - MARGIN - SPACE));
|
2012-08-09 10:45:11 +08:00
|
|
|
menuRequest->addChild(itemGet);
|
|
|
|
|
|
|
|
// Post
|
|
|
|
CCLabelTTF *labelPost = CCLabelTTF::create("Test Post", "Arial", 22);
|
2013-06-14 11:36:43 +08:00
|
|
|
CCMenuItemLabel *itemPost = CCMenuItemLabel::create(labelPost, CC_CALLBACK_1(HttpClientTest::onMenuPostTestClicked, this));
|
2013-04-22 00:23:27 +08:00
|
|
|
itemPost->setPosition(ccp(winSize.width / 2, winSize.height - MARGIN - 2 * SPACE));
|
2012-08-09 10:45:11 +08:00
|
|
|
menuRequest->addChild(itemPost);
|
|
|
|
|
|
|
|
// Post Binary
|
|
|
|
CCLabelTTF *labelPostBinary = CCLabelTTF::create("Test Post Binary", "Arial", 22);
|
2013-06-14 11:36:43 +08:00
|
|
|
CCMenuItemLabel *itemPostBinary = CCMenuItemLabel::create(labelPostBinary, CC_CALLBACK_1(HttpClientTest::onMenuPostBinaryTestClicked, this));
|
2013-04-22 00:23:27 +08:00
|
|
|
itemPostBinary->setPosition(ccp(winSize.width / 2, winSize.height - MARGIN - 3 * SPACE));
|
2012-08-09 10:45:11 +08:00
|
|
|
menuRequest->addChild(itemPostBinary);
|
2013-04-22 00:23:27 +08:00
|
|
|
|
|
|
|
// Put
|
|
|
|
CCLabelTTF *labelPut = CCLabelTTF::create("Test Put", "Arial", 22);
|
2013-06-14 11:36:43 +08:00
|
|
|
CCMenuItemLabel *itemPut = CCMenuItemLabel::create(labelPut, CC_CALLBACK_1(HttpClientTest::onMenuPutTestClicked, this));
|
2013-04-22 00:23:27 +08:00
|
|
|
itemPut->setPosition(ccp(winSize.width / 2, winSize.height - MARGIN - 4 * SPACE));
|
|
|
|
menuRequest->addChild(itemPut);
|
|
|
|
|
|
|
|
// Delete
|
|
|
|
CCLabelTTF *labelDelete = CCLabelTTF::create("Test Delete", "Arial", 22);
|
2013-06-14 11:36:43 +08:00
|
|
|
CCMenuItemLabel *itemDelete = CCMenuItemLabel::create(labelDelete, CC_CALLBACK_1(HttpClientTest::onMenuDeleteTestClicked, this));
|
2013-04-22 00:23:27 +08:00
|
|
|
itemDelete->setPosition(ccp(winSize.width / 2, winSize.height - MARGIN - 5 * SPACE));
|
|
|
|
menuRequest->addChild(itemDelete);
|
2012-08-09 10:45:11 +08:00
|
|
|
|
|
|
|
// Response Code Label
|
2013-06-15 14:03:30 +08:00
|
|
|
_labelStatusCode = CCLabelTTF::create("HTTP Status Code", "Marker Felt", 20);
|
|
|
|
_labelStatusCode->setPosition(ccp(winSize.width / 2, winSize.height - MARGIN - 6 * SPACE));
|
|
|
|
addChild(_labelStatusCode);
|
2012-08-09 10:45:11 +08:00
|
|
|
|
|
|
|
// Back Menu
|
2013-06-14 11:36:43 +08:00
|
|
|
CCMenuItemFont *itemBack = CCMenuItemFont::create("Back", CC_CALLBACK_1(HttpClientTest::toExtensionsMainLayer, this));
|
2012-10-23 17:48:50 +08:00
|
|
|
itemBack->setPosition(ccp(VisibleRect::rightBottom().x - 50, VisibleRect::rightBottom().y + 25));
|
2012-08-09 10:45:11 +08:00
|
|
|
CCMenu *menuBack = CCMenu::create(itemBack, NULL);
|
|
|
|
menuBack->setPosition(CCPointZero);
|
|
|
|
addChild(menuBack);
|
|
|
|
}
|
|
|
|
|
|
|
|
HttpClientTest::~HttpClientTest()
|
|
|
|
{
|
|
|
|
CCHttpClient::getInstance()->destroyInstance();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HttpClientTest::onMenuGetTestClicked(cocos2d::CCObject *sender)
|
|
|
|
{
|
|
|
|
// test 1
|
|
|
|
{
|
|
|
|
CCHttpRequest* request = new CCHttpRequest();
|
|
|
|
request->setUrl("http://just-make-this-request-failed.com");
|
|
|
|
request->setRequestType(CCHttpRequest::kHttpGet);
|
2013-04-28 22:42:31 +08:00
|
|
|
request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted));
|
2012-08-09 10:45:11 +08:00
|
|
|
request->setTag("GET test1");
|
|
|
|
CCHttpClient::getInstance()->send(request);
|
|
|
|
request->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
// test 2
|
|
|
|
{
|
|
|
|
CCHttpRequest* request = new CCHttpRequest();
|
|
|
|
// required fields
|
2013-05-07 15:13:55 +08:00
|
|
|
request->setUrl("http://httpbin.org/ip");
|
2012-08-09 10:45:11 +08:00
|
|
|
request->setRequestType(CCHttpRequest::kHttpGet);
|
2013-04-28 22:42:31 +08:00
|
|
|
request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted));
|
2012-08-09 10:45:11 +08:00
|
|
|
// optional fields
|
|
|
|
request->setTag("GET test2");
|
|
|
|
|
|
|
|
CCHttpClient::getInstance()->send(request);
|
|
|
|
|
|
|
|
// don't forget to release it, pair to new
|
|
|
|
request->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
// test 3
|
|
|
|
{
|
|
|
|
CCHttpRequest* request = new CCHttpRequest();
|
2013-05-07 15:13:55 +08:00
|
|
|
request->setUrl("http://httpbin.org/get");
|
2012-08-09 10:45:11 +08:00
|
|
|
request->setRequestType(CCHttpRequest::kHttpGet);
|
2013-04-28 22:42:31 +08:00
|
|
|
request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted));
|
2012-08-09 10:45:11 +08:00
|
|
|
request->setTag("GET test3");
|
|
|
|
CCHttpClient::getInstance()->send(request);
|
|
|
|
request->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
// waiting
|
2013-06-15 14:03:30 +08:00
|
|
|
_labelStatusCode->setString("waiting...");
|
2012-08-09 10:45:11 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void HttpClientTest::onMenuPostTestClicked(cocos2d::CCObject *sender)
|
|
|
|
{
|
2012-09-24 17:15:53 +08:00
|
|
|
// test 1
|
|
|
|
{
|
|
|
|
CCHttpRequest* request = new CCHttpRequest();
|
2013-05-07 15:13:55 +08:00
|
|
|
request->setUrl("http://httpbin.org/post");
|
2012-09-24 17:15:53 +08:00
|
|
|
request->setRequestType(CCHttpRequest::kHttpPost);
|
2013-04-28 22:42:31 +08:00
|
|
|
request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted));
|
2012-09-24 17:15:53 +08:00
|
|
|
|
|
|
|
// write the post data
|
|
|
|
const char* postData = "visitor=cocos2d&TestSuite=Extensions Test/NetworkTest";
|
|
|
|
request->setRequestData(postData, strlen(postData));
|
|
|
|
|
|
|
|
request->setTag("POST test1");
|
|
|
|
CCHttpClient::getInstance()->send(request);
|
|
|
|
request->release();
|
|
|
|
}
|
2012-08-09 10:45:11 +08:00
|
|
|
|
2012-09-24 17:15:53 +08:00
|
|
|
// test 2: set Content-Type
|
|
|
|
{
|
|
|
|
CCHttpRequest* request = new CCHttpRequest();
|
2013-05-07 15:13:55 +08:00
|
|
|
request->setUrl("http://httpbin.org/post");
|
2012-09-24 17:15:53 +08:00
|
|
|
request->setRequestType(CCHttpRequest::kHttpPost);
|
|
|
|
std::vector<std::string> headers;
|
|
|
|
headers.push_back("Content-Type: application/json; charset=utf-8");
|
|
|
|
request->setHeaders(headers);
|
2013-04-28 22:42:31 +08:00
|
|
|
request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted));
|
2012-09-24 17:15:53 +08:00
|
|
|
|
|
|
|
// write the post data
|
|
|
|
const char* postData = "visitor=cocos2d&TestSuite=Extensions Test/NetworkTest";
|
|
|
|
request->setRequestData(postData, strlen(postData));
|
|
|
|
|
|
|
|
request->setTag("POST test2");
|
|
|
|
CCHttpClient::getInstance()->send(request);
|
|
|
|
request->release();
|
|
|
|
}
|
2012-08-09 10:45:11 +08:00
|
|
|
|
|
|
|
// waiting
|
2013-06-15 14:03:30 +08:00
|
|
|
_labelStatusCode->setString("waiting...");
|
2012-08-09 10:45:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void HttpClientTest::onMenuPostBinaryTestClicked(cocos2d::CCObject *sender)
|
|
|
|
{
|
|
|
|
CCHttpRequest* request = new CCHttpRequest();
|
2013-05-07 15:13:55 +08:00
|
|
|
request->setUrl("http://httpbin.org/post");
|
2012-08-09 10:45:11 +08:00
|
|
|
request->setRequestType(CCHttpRequest::kHttpPost);
|
2013-04-28 22:42:31 +08:00
|
|
|
request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted));
|
2012-08-09 10:45:11 +08:00
|
|
|
|
|
|
|
// write the post data
|
|
|
|
char postData[22] = "binary=hello\0\0cocos2d"; // including \0, the strings after \0 should not be cut in response
|
|
|
|
request->setRequestData(postData, 22);
|
|
|
|
|
|
|
|
request->setTag("POST Binary test");
|
|
|
|
CCHttpClient::getInstance()->send(request);
|
|
|
|
request->release();
|
|
|
|
|
|
|
|
// waiting
|
2013-06-15 14:03:30 +08:00
|
|
|
_labelStatusCode->setString("waiting...");
|
2012-08-09 10:45:11 +08:00
|
|
|
}
|
|
|
|
|
2013-05-07 15:37:25 +08:00
|
|
|
|
|
|
|
|
2013-04-22 00:23:27 +08:00
|
|
|
void HttpClientTest::onMenuPutTestClicked(CCObject *sender)
|
|
|
|
{
|
|
|
|
// test 1
|
|
|
|
{
|
|
|
|
CCHttpRequest* request = new CCHttpRequest();
|
2013-05-07 15:37:25 +08:00
|
|
|
request->setUrl("http://httpbin.org/put");
|
2013-04-22 00:23:27 +08:00
|
|
|
request->setRequestType(CCHttpRequest::kHttpPut);
|
2013-05-07 15:37:25 +08:00
|
|
|
request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted));
|
2013-04-22 00:23:27 +08:00
|
|
|
|
|
|
|
// write the post data
|
|
|
|
const char* postData = "visitor=cocos2d&TestSuite=Extensions Test/NetworkTest";
|
|
|
|
request->setRequestData(postData, strlen(postData));
|
|
|
|
|
|
|
|
request->setTag("PUT test1");
|
|
|
|
CCHttpClient::getInstance()->send(request);
|
|
|
|
request->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
// test 2: set Content-Type
|
|
|
|
{
|
|
|
|
CCHttpRequest* request = new CCHttpRequest();
|
2013-05-07 15:37:25 +08:00
|
|
|
request->setUrl("http://httpbin.org/put");
|
2013-04-22 00:23:27 +08:00
|
|
|
request->setRequestType(CCHttpRequest::kHttpPut);
|
|
|
|
std::vector<std::string> headers;
|
|
|
|
headers.push_back("Content-Type: application/json; charset=utf-8");
|
|
|
|
request->setHeaders(headers);
|
2013-05-07 15:37:25 +08:00
|
|
|
request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted));
|
2013-04-22 00:23:27 +08:00
|
|
|
|
|
|
|
// write the post data
|
|
|
|
const char* postData = "visitor=cocos2d&TestSuite=Extensions Test/NetworkTest";
|
|
|
|
request->setRequestData(postData, strlen(postData));
|
|
|
|
|
|
|
|
request->setTag("PUT test2");
|
|
|
|
CCHttpClient::getInstance()->send(request);
|
|
|
|
request->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
// waiting
|
2013-06-15 14:03:30 +08:00
|
|
|
_labelStatusCode->setString("waiting...");
|
2013-04-22 00:23:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void HttpClientTest::onMenuDeleteTestClicked(CCObject *sender)
|
|
|
|
{
|
|
|
|
// test 1
|
|
|
|
{
|
|
|
|
CCHttpRequest* request = new CCHttpRequest();
|
|
|
|
request->setUrl("http://just-make-this-request-failed.com");
|
|
|
|
request->setRequestType(CCHttpRequest::kHttpDelete);
|
2013-05-07 15:37:25 +08:00
|
|
|
request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted));
|
2013-04-22 00:23:27 +08:00
|
|
|
request->setTag("DELETE test1");
|
|
|
|
CCHttpClient::getInstance()->send(request);
|
|
|
|
request->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
// test 2
|
|
|
|
{
|
|
|
|
CCHttpRequest* request = new CCHttpRequest();
|
2013-05-07 15:37:25 +08:00
|
|
|
request->setUrl("http://httpbin.org/delete");
|
2013-04-22 00:23:27 +08:00
|
|
|
request->setRequestType(CCHttpRequest::kHttpDelete);
|
2013-05-07 15:37:25 +08:00
|
|
|
request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted));
|
2013-04-22 00:23:27 +08:00
|
|
|
request->setTag("DELETE test2");
|
|
|
|
CCHttpClient::getInstance()->send(request);
|
|
|
|
request->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
// waiting
|
2013-06-15 14:03:30 +08:00
|
|
|
_labelStatusCode->setString("waiting...");
|
2013-04-22 00:23:27 +08:00
|
|
|
}
|
|
|
|
|
2013-04-28 22:42:31 +08:00
|
|
|
void HttpClientTest::onHttpRequestCompleted(CCHttpClient *sender, CCHttpResponse *response)
|
2012-08-09 10:45:11 +08:00
|
|
|
{
|
|
|
|
if (!response)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// You can get original request type from: response->request->reqType
|
|
|
|
if (0 != strlen(response->getHttpRequest()->getTag()))
|
|
|
|
{
|
|
|
|
CCLog("%s completed", response->getHttpRequest()->getTag());
|
|
|
|
}
|
|
|
|
|
|
|
|
int statusCode = response->getResponseCode();
|
|
|
|
char statusString[64] = {};
|
|
|
|
sprintf(statusString, "HTTP Status Code: %d, tag = %s", statusCode, response->getHttpRequest()->getTag());
|
2013-06-15 14:03:30 +08:00
|
|
|
_labelStatusCode->setString(statusString);
|
2012-08-09 10:45:11 +08:00
|
|
|
CCLog("response code: %d", statusCode);
|
|
|
|
|
|
|
|
if (!response->isSucceed())
|
|
|
|
{
|
|
|
|
CCLog("response failed");
|
|
|
|
CCLog("error buffer: %s", response->getErrorBuffer());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// dump data
|
|
|
|
std::vector<char> *buffer = response->getResponseData();
|
|
|
|
printf("Http Test, dump data: ");
|
2012-08-24 14:35:25 +08:00
|
|
|
for (unsigned int i = 0; i < buffer->size(); i++)
|
2012-08-09 10:45:11 +08:00
|
|
|
{
|
|
|
|
printf("%c", (*buffer)[i]);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void HttpClientTest::toExtensionsMainLayer(cocos2d::CCObject *sender)
|
|
|
|
{
|
|
|
|
ExtensionsTestScene *pScene = new ExtensionsTestScene();
|
|
|
|
pScene->runThisTest();
|
|
|
|
pScene->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void runHttpClientTest()
|
|
|
|
{
|
|
|
|
CCScene *pScene = CCScene::create();
|
|
|
|
HttpClientTest *pLayer = new HttpClientTest();
|
|
|
|
pScene->addChild(pLayer);
|
|
|
|
|
|
|
|
CCDirector::sharedDirector()->replaceScene(pScene);
|
|
|
|
pLayer->release();
|
|
|
|
}
|