Cocos2d-x  v3.8
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Groups Pages
HttpRequest Class Reference

Defines the object which users must packed for HttpClient::send(HttpRequest*) method. More...

Inherits Ref.

Classes

class  _prxy
 This sub class is just for migration SEL_CallFuncND to SEL_HttpResponse,someday this way will be removed. More...
 

Public Types

enum  Type
 The HttpRequest type enum used in the HttpRequest::setRequestType.
 

Public Member Functions

 HttpRequest ()
 Constructor. More...
 
virtual ~HttpRequest ()
 Destructor. More...
 
Refautorelease (void)
 Override autorelease method to avoid developers to call it. More...
 
void setRequestType (Type type)
 Set request type of HttpRequest object before being sent,now it support the enum value of HttpRequest::Type. More...
 
Type getRequestType ()
 Get the request type of HttpRequest object. More...
 
void setUrl (const char *url)
 Set the url address of HttpRequest object. More...
 
const char * getUrl ()
 Get the url address of HttpRequest object. More...
 
void setRequestData (const char *buffer, size_t len)
 Set the request data of HttpRequest object. More...
 
char * getRequestData ()
 Get the request data pointer of HttpRequest object. More...
 
ssize_t getRequestDataSize ()
 Get the size of request data. More...
 
void setTag (const char *tag)
 Set a string tag to identify your request. More...
 
const char * getTag ()
 Get the string tag to identify the request. More...
 
void setUserData (void *pUserData)
 Set user-customed data of HttpRequest object. More...
 
void * getUserData ()
 Get the user-customed data pointer which were pre-setted. More...
 
void setResponseCallback (Ref *pTarget, SEL_CallFuncND pSelector)
 Set the target and related callback selector. More...
 
void setResponseCallback (Ref *pTarget, SEL_HttpResponse pSelector)
 Set the target and related callback selector of HttpRequest object. More...
 
void setResponseCallback (const ccHttpRequestCallback &callback)
 Set response callback function of HttpRequest object. More...
 
RefgetTarget ()
 Get the target of callback selector funtion, mainly used by HttpClient. More...
 
_prxy getSelector ()
 Get _prxy object by the _pSelector. More...
 
const ccHttpRequestCallback & getCallback ()
 Get ccHttpRequestCallback callback function. More...
 
void setHeaders (std::vector< std::string > pHeaders)
 Set custom-defined headers. More...
 
std::vector< std::string > getHeaders ()
 Get custom headers. More...
 
- Public Member Functions inherited from Ref
void retain ()
 Retains the ownership. More...
 
void release ()
 Releases the ownership immediately. More...
 
Refautorelease ()
 Releases the ownership sometime soon automatically. More...
 
unsigned int getReferenceCount () const
 Returns the Ref's current reference count. More...
 
virtual ~Ref ()
 Destructor. More...
 

Additional Inherited Members

- Public Attributes inherited from Ref
unsigned int _ID
 object id, ScriptSupport need public _ID
 
int _luaID
 Lua reference id.
 
void * _scriptObject
 scriptObject, support for swift
 

Detailed Description

Defines the object which users must packed for HttpClient::send(HttpRequest*) method.

Please refer to tests/test-cpp/Classes/ExtensionTest/NetworkTest/HttpClientTest.cpp as a sample

Since
v2.0.2

Constructor & Destructor Documentation

HttpRequest ( )
inline

Constructor.

Because HttpRequest object will be used between UI thead and network thread, requestObj->autorelease() is forbidden to avoid crashes in AutoreleasePool new/retain/release still works, which means you need to release it manually Please refer to HttpRequestTest.cpp to find its usage.

virtual ~HttpRequest ( )
inlinevirtual

Destructor.

Member Function Documentation

Ref* autorelease ( void  )
inline

Override autorelease method to avoid developers to call it.

If this function was called, it would trigger assert in debug mode

Returns
Ref* always return nullptr.
void setRequestType ( Type  type)
inline

Set request type of HttpRequest object before being sent,now it support the enum value of HttpRequest::Type.

Parameters
typethe request type.
Type getRequestType ( )
inline

Get the request type of HttpRequest object.

Returns
HttpRequest::Type.
void setUrl ( const char *  url)
inline

Set the url address of HttpRequest object.

The url value could be like these: "http://httpbin.org/ip" or "https://httpbin.org/get"

Parameters
urlthe string pointer.
const char* getUrl ( )
inline

Get the url address of HttpRequest object.

Returns
const char* the pointer of _url.
void setRequestData ( const char *  buffer,
size_t  len 
)
inline

Set the request data of HttpRequest object.

Parameters
bufferthe buffer of request data, it support binary data.
lenthe size of request data.
char* getRequestData ( )
inline

Get the request data pointer of HttpRequest object.

Returns
char* the request data pointer.
ssize_t getRequestDataSize ( )
inline

Get the size of request data.

Returns
ssize_t the size of request data
void setTag ( const char *  tag)
inline

Set a string tag to identify your request.

This tag can be found in HttpResponse->getHttpRequest->getTag().

Parameters
tagthe string pointer
const char* getTag ( )
inline

Get the string tag to identify the request.

The best practice is to use it in your MyClass::onMyHttpRequestCompleted(sender, HttpResponse*) callback.

Returns
const char* the pointer of _tag
void setUserData ( void *  pUserData)
inline

Set user-customed data of HttpRequest object.

You can attach a customed data in each request, and get it back in response callback. But you need to new/delete the data pointer manully.

Parameters
pUserDatathe string pointer
void* getUserData ( )
inline

Get the user-customed data pointer which were pre-setted.

Don't forget to delete it. HttpClient/HttpResponse/HttpRequest will do nothing with this pointer.

Returns
void* the pointer of user-customed data.
void setResponseCallback ( Ref pTarget,
SEL_CallFuncND  pSelector 
)
inline

Set the target and related callback selector.

When response come back, it would call (pTarget->*pSelector) to process something.

Parameters
pTargetthe target object pointer.
pSelectorthe callback function.
void setResponseCallback ( Ref pTarget,
SEL_HttpResponse  pSelector 
)
inline

Set the target and related callback selector of HttpRequest object.

When response come back, we would call (pTarget->*pSelector) to process response data.

Parameters
pTargetthe target object pointer.
pSelectorthe SEL_HttpResponse function.
void setResponseCallback ( const ccHttpRequestCallback &  callback)
inline

Set response callback function of HttpRequest object.

When response come back, we would call _pCallback to process response data.

Parameters
callbackthe ccHttpRequestCallback function.
Ref* getTarget ( )
inline

Get the target of callback selector funtion, mainly used by HttpClient.

Returns
Ref* the target of callback selector funtion
_prxy getSelector ( )
inline

Get _prxy object by the _pSelector.

Returns
_prxy the _prxy object
const ccHttpRequestCallback&
getCallback
( )
inline

Get ccHttpRequestCallback callback function.

Returns
const ccHttpRequestCallback& ccHttpRequestCallback callback function.
void setHeaders ( std::vector< std::string >  pHeaders)
inline

Set custom-defined headers.

Parameters
pHeadersthe string vector of custom-defined headers.
std::vector<std::string> getHeaders ( )
inline

Get custom headers.

Returns
std::vector<std::string> the string vector of custom-defined headers.

The documentation for this class was generated from the following file: