HttpRequest(联网 http请求)

2024-01-30 21:18
文章标签 http 请求 联网 httprequest

本文主要是介绍HttpRequest(联网 http请求),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

         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__


这篇关于HttpRequest(联网 http请求)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/661582

相关文章

Maven 配置中的 <mirror>绕过 HTTP 阻断机制的方法

《Maven配置中的<mirror>绕过HTTP阻断机制的方法》:本文主要介绍Maven配置中的<mirror>绕过HTTP阻断机制的方法,本文给大家分享问题原因及解决方案,感兴趣的朋友一... 目录一、问题场景:升级 Maven 后构建失败二、解决方案:通过 <mirror> 配置覆盖默认行为1. 配置示

Linux中修改Apache HTTP Server(httpd)默认端口的完整指南

《Linux中修改ApacheHTTPServer(httpd)默认端口的完整指南》ApacheHTTPServer(简称httpd)是Linux系统中最常用的Web服务器之一,本文将详细介绍如何... 目录一、修改 httpd 默认端口的步骤1. 查找 httpd 配置文件路径2. 编辑配置文件3. 保存

python web 开发之Flask中间件与请求处理钩子的最佳实践

《pythonweb开发之Flask中间件与请求处理钩子的最佳实践》Flask作为轻量级Web框架,提供了灵活的请求处理机制,中间件和请求钩子允许开发者在请求处理的不同阶段插入自定义逻辑,实现诸如... 目录Flask中间件与请求处理钩子完全指南1. 引言2. 请求处理生命周期概述3. 请求钩子详解3.1

C++ HTTP框架推荐(特点及优势)

《C++HTTP框架推荐(特点及优势)》:本文主要介绍C++HTTP框架推荐的相关资料,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. Crow2. Drogon3. Pistache4. cpp-httplib5. Beast (Boos

SpringBoot中HTTP连接池的配置与优化

《SpringBoot中HTTP连接池的配置与优化》这篇文章主要为大家详细介绍了SpringBoot中HTTP连接池的配置与优化的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录一、HTTP连接池的核心价值二、Spring Boot集成方案方案1:Apache HttpCl

Spring Boot Controller处理HTTP请求体的方法

《SpringBootController处理HTTP请求体的方法》SpringBoot提供了强大的机制来处理不同Content-Type​的HTTP请求体,这主要依赖于HttpMessageCo... 目录一、核心机制:HttpMessageConverter​二、按Content-Type​处理详解1.

一文详解如何在Vue3中封装API请求

《一文详解如何在Vue3中封装API请求》在现代前端开发中,API请求是不可避免的一部分,尤其是与后端交互时,下面我们来看看如何在Vue3项目中封装API请求,让你在实现功能时更加高效吧... 目录为什么要封装API请求1. vue 3项目结构2. 安装axIOS3. 创建API封装模块4. 封装API请求

SpringBoot请求参数接收控制指南分享

《SpringBoot请求参数接收控制指南分享》:本文主要介绍SpringBoot请求参数接收控制指南,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Spring Boot 请求参数接收控制指南1. 概述2. 有注解时参数接收方式对比3. 无注解时接收参数默认位置

Spring 请求之传递 JSON 数据的操作方法

《Spring请求之传递JSON数据的操作方法》JSON就是一种数据格式,有自己的格式和语法,使用文本表示一个对象或数组的信息,因此JSON本质是字符串,主要负责在不同的语言中数据传递和交换,这... 目录jsON 概念JSON 语法JSON 的语法JSON 的两种结构JSON 字符串和 Java 对象互转

SpringMVC获取请求参数的方法

《SpringMVC获取请求参数的方法》:本文主要介绍SpringMVC获取请求参数的方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下... 目录1、通过ServletAPI获取2、通过控制器方法的形参获取请求参数3、@RequestParam4、@