我是cocos2dx
开发人员。在此代码中,服务器抛出错误,您的数据不是GZIP
格式。帮助我如何转换Gzip格式!
void HelloWorld::requestToserver()
{
string postData ="client side request";
// Setting HTTP Request Header
std::vector<std::string> headers;
vector<string> pHeraders;
pHeraders.push_back("Accept-Encoding:gzip");
pHeraders.push_back("Content-Type:application/json");
pHeraders.push_back("Content-Encoding:gzip");
// Creating a URL
cocos2d::extension::CCHttpRequest * request = new cocos2d::extension::CCHttpRequest();
request->setUrl("http://192.168.1.47:8080/PicL…/PlayerRecordClientInteraction");
// If you want to use GET method, replace kHttpPost to kHttpGet
request->setRequestType(cocos2d::extension::CCHttpRequest::kHttpPost);
request->setResponseCallback(this, httpresponse_selector(HelloWorld::onHttpRequestCompleted));
request->setRequestData(postData, strlen(postData));
request->setHeaders(headers);
request->setTag("POST Request");
// Execute
cocos2d::extension::CCHttpClient::getInstance()->send(request);
// Release the Connection
request->release();
}
void DailyPuzzle::onHttpRequestCompleted(cocos2d::CCNode * sender, void * data)
{
HttpResponse * response = (HttpResponse *)data;
if (!response)
{
return;
}
if (0 != strlen(response->getHttpRequest()->getTag()))
{
CCLog("%s completed", response->getHttpRequest()->getTag());
}
// Check the HTTP Status Code
int statusCode = response->getResponseCode();
char statusString[64] = {};
sprintf(statusString, "HTTP Status Code: %d, tag = %s", statusCode, response->getHttpRequest()->getTag());
CCLog("response code: %d", statusCode);
// A connection failure
if (!response->isSucceed())
{
CCLog("response failed");
CCLog("error buffer: %s", response->getErrorBuffer());
return;
}
// The data will be placed in the buffer.
std::vector<char> * buffer = response->getResponseData();
char * concatenated = (char *) malloc(buffer->size() + 1);
std::string s2(buffer->begin(), buffer->end());
strcpy(concatenated, s2.c_str());
}