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

相关文章

SpringMVC获取请求参数的方法

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

鸿蒙中Axios数据请求的封装和配置方法

《鸿蒙中Axios数据请求的封装和配置方法》:本文主要介绍鸿蒙中Axios数据请求的封装和配置方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1.配置权限 应用级权限和系统级权限2.配置网络请求的代码3.下载在Entry中 下载AxIOS4.封装Htt

springboot filter实现请求响应全链路拦截

《springbootfilter实现请求响应全链路拦截》这篇文章主要为大家详细介绍了SpringBoot如何结合Filter同时拦截请求和响应,从而实现​​日志采集自动化,感兴趣的小伙伴可以跟随小... 目录一、为什么你需要这个过滤器?​​​二、核心实现:一个Filter搞定双向数据流​​​​三、完整代码

Nginx中配置HTTP/2协议的详细指南

《Nginx中配置HTTP/2协议的详细指南》HTTP/2是HTTP协议的下一代版本,旨在提高性能、减少延迟并优化现代网络环境中的通信效率,本文将为大家介绍Nginx配置HTTP/2协议想详细步骤,需... 目录一、HTTP/2 协议概述1.HTTP/22. HTTP/2 的核心特性3. HTTP/2 的优

AJAX请求上传下载进度监控实现方式

《AJAX请求上传下载进度监控实现方式》在日常Web开发中,AJAX(AsynchronousJavaScriptandXML)被广泛用于异步请求数据,而无需刷新整个页面,:本文主要介绍AJAX请... 目录1. 前言2. 基于XMLHttpRequest的进度监控2.1 基础版文件上传监控2.2 增强版多

使用Python自建轻量级的HTTP调试工具

《使用Python自建轻量级的HTTP调试工具》这篇文章主要为大家详细介绍了如何使用Python自建一个轻量级的HTTP调试工具,文中的示例代码讲解详细,感兴趣的小伙伴可以参考一下... 目录一、为什么需要自建工具二、核心功能设计三、技术选型四、分步实现五、进阶优化技巧六、使用示例七、性能对比八、扩展方向建

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

SpringBoot使用OkHttp完成高效网络请求详解

《SpringBoot使用OkHttp完成高效网络请求详解》OkHttp是一个高效的HTTP客户端,支持同步和异步请求,且具备自动处理cookie、缓存和连接池等高级功能,下面我们来看看SpringB... 目录一、OkHttp 简介二、在 Spring Boot 中集成 OkHttp三、封装 OkHttp

Go语言中最便捷的http请求包resty的使用详解

《Go语言中最便捷的http请求包resty的使用详解》go语言虽然自身就有net/http包,但是说实话用起来没那么好用,resty包是go语言中一个非常受欢迎的http请求处理包,下面我们一起来学... 目录安装一、一个简单的get二、带查询参数三、设置请求头、body四、设置表单数据五、处理响应六、超

Python使用DeepSeek进行联网搜索功能详解

《Python使用DeepSeek进行联网搜索功能详解》Python作为一种非常流行的编程语言,结合DeepSeek这一高性能的深度学习工具包,可以方便地处理各种深度学习任务,本文将介绍一下如何使用P... 目录一、环境准备与依赖安装二、DeepSeek简介三、联网搜索与数据集准备四、实践示例:图像分类1.