当前位置: 首页 > 工具软件 > http-request > 使用案例 >

HttpRequest(联网 http请求)

荆乐
2023-12-01

         cocos2d-x学习篇之网络(http)篇         


#ifndef __HTTP_REQUEST_H__

#define __HTTP_REQUEST_H__


#include "cocos2d.h"

#include "ExtensionMacros.h"


NS_CC_EXT_BEGIN


class CCHttpClient;

class CCHttpResponse;

typedef void (CCObject::*SEL_HttpResponse)(CCHttpClient* client, CCHttpResponse* response); //定义类型SEL_HttpResponse(CCObject类内函数 参数CCHttpClient CCHttpResponse 返回void 

#define httpresponse_selector(_SELECTOR) (cocos2d::extension::SEL_HttpResponse)(&_SELECTOR)//定义宏httpresponse_selector 可以将参数强转成cocos2d::extension::SEL_HttpResponse类型  


/** 

 @brief defines the object which users must packed(包装) for CCHttpClient::send(HttpRequest*) method.

 Please refer to samples/TestCpp/Classes/ExtensionTest/NetworkTest/HttpClientTest.cpp as a sample

 @since v2.0.2

 */


class CCHttpRequest : public CCObject

{

public:

    /** Use this enum type as param in setReqeustType(param) */

    typedef enum

    {

        kHttpGet,

        kHttpPost,

        kHttpPut,

        kHttpDelete,

        kHttpUnkown,

    } HttpRequestType;

    

    /** Constructor 

        Because HttpRequest object will be used between UI thead and network thread,

        requestObj->autorelease() is forbidden to avoid crashes in CCAutoreleasePool

        new/retain/release still works, which means you need to release it manually

        Please refer to HttpRequestTest.cpp to find its usage

     */

    CCHttpRequest()

    {

        _requestType = kHttpUnkown;

        _url.clear();

        _requestData.clear();

        _tag.clear();

        _pTarget = NULL;

        _pSelector = NULL;

        _pUserData = NULL;

    };

    

    /** Destructor */

    virtual ~CCHttpRequest()

    {

        if (_pTarget)

        {

            _pTarget->release();

        }

    };

    

    /** Override autorelease method to avoid developers to call it */

    CCObject* autorelease(void)

    {

        CCAssert(false, "HttpResponse is used between network thread and ui thread \

                 therefore, autorelease is forbidden here");

        return NULL;

    }

            

    // setter/getters for properties

     

    /** Required field(必须填写) for HttpRequest object before being sent.

        kHttpGet & kHttpPost is currently supported

     */

    inline void setRequestType(HttpRequestType type) // 必须填写 设置请求类型 kHttpGet  kHttpPost当前可用

    {

        _requestType = type;

    };

    /** Get back the kHttpGet/Post/... enum value */

    inline HttpRequestType getRequestType()

    {

        return _requestType;

    };

    

    /** Required field for HttpRequest object before being sent.

     */

    inline void setUrl(const char* url) //设置url 必须填写

    {

        _url = url;

    };

    /** Get back the setted url */

    inline const char* getUrl()

    {

        return _url.c_str();

    };

    

    /** Option field(选择字段). You can set your post data here

     */

    inline void setRequestData(const char* buffer, unsigned int len)  //kHttpPost 类型可以在url之外传额外信息  kHttpGet传的所有东西都在url显示(不安全)

    {

        _requestData.assign(buffer, buffer + len);

    };

    /** Get the request data pointer back */

    inline char* getRequestData()

    {

        return &(_requestData.front());

    }

    /** Get the size of request data back */

    inline int getRequestDataSize()

    {

        return _requestData.size();

    }

    

    /** Option field(选择字段) . You can set a string tag to identify(识别) your request, this tag can be found in HttpResponse->getHttpRequest->getTag()

     */

    inline void setTag(const char* tag) //用于辨别哪个请求

    {

        _tag = tag;

    };

    /** Get the string tag back to identify the request. 

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

     */

    inline const char* getTag()

    {

        return _tag.c_str();

    };

    

    /** Option field. 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

     */

    inline void setUserData(void* pUserData)

    {

        _pUserData = pUserData;

    };

    /** Get the pre-setted custom data pointer back.

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

     */

    inline void* getUserData()

    {

        return _pUserData;

    };

    

    /** Required field(必须填写). You should set the callback selector function at ack(确认) the http request completed(完全的

     */

    CC_DEPRECATED_ATTRIBUTE inline void setResponseCallback(CCObject* pTarget, SEL_CallFuncND pSelector)

    {

        setResponseCallback(pTarget, (SEL_HttpResponse) pSelector);

    }


    inline void setResponseCallback(CCObject* pTarget, SEL_HttpResponse pSelector)

    {

        _pTarget = pTarget;

        _pSelector = pSelector;

        

        if (_pTarget)

        {

            _pTarget->retain();

        }

    }    

    /** Get the target of callback selector funtion, mainly used by CCHttpClient */

    inline CCObject* getTarget()

    {

        return _pTarget;

    }


    /* This sub class is just for migration(迁移) SEL_CallFuncND to SEL_HttpResponse, 

       someday this way will be removed */    //这个类用于SEL_CallFuncND SEL_HttpResponse之间转换 将废弃

    class _prxy

    {

    public:

        _prxy( SEL_HttpResponse cb ) :_cb(cb) {}

        ~_prxy(){};

        operator SEL_HttpResponse() const { return _cb; }

        CC_DEPRECATED_ATTRIBUTE operator SEL_CallFuncND()   const { return (SEL_CallFuncND) _cb; }

    protected:

        SEL_HttpResponse _cb;

    };

    

    /** Get the selector function pointer, mainly used by CCHttpClient */

    inline _prxy getSelector()

    {

        return _prxy(_pSelector);

    }

    

    /** Set any custom headers **/

    inline void setHeaders(std::vector<std::string> pHeaders)

  {

  _headers=pHeaders;

  }

   

    /** Get custom headers **/

  inline std::vector<std::string> getHeaders()

  {

  return _headers;

  }



protected:

    // properties

    HttpRequestType             _requestType;    /// kHttpRequestGet, kHttpRequestPost or other enums

    std::string                 _url;            /// target url that this request is sent to

    std::vector<char>           _requestData;    /// used for POST

    std::string                 _tag;            /// user defined tag, to identify different requests in response callback

    CCObject*          _pTarget;        /// callback target of pSelector function

    SEL_HttpResponse            _pSelector;      /// callback function, e.g. MyLayer::onHttpResponse(CCHttpClient *sender, CCHttpResponse * response)

    void*                       _pUserData;      /// You can add your customed data here 

    std::vector<std::string>    _headers;      /// custom http headers

};


NS_CC_EXT_END


#endif //__HTTP_REQUEST_H__


 类似资料: