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

相关文章

axios全局封装AbortController取消重复请求

为什么? 问题:为什么axios要配置AbortController?防抖节流不行吗? 分析: 防抖节流本质上是用延时器来操作请求的。防抖是判断延时器是否存在,如果存在,清除延时器,重新开启一个延时器,只执行最后一次请求。节流呢,是判断延时器是否存在,如果存在,直接return掉,直到执行完这个延时器。事实上,这些体验感都不算友好,因为对于用户来说,得等一些时间,尤其是首次请求,不是那么流畅

微服务中RPC的强类型检查与HTTP的弱类型对比

在微服务架构中,服务间的通信是一个至关重要的环节。其中,远程过程调用(RPC)和HTTP是两种最常见的通信方式。虽然它们都能实现服务间的数据交换,但在类型检查方面,RPC的强类型检查和HTTP的弱类型之间有着显著的差异。本文将深入探讨这两种通信方式在类型检查方面的优缺点,以及它们对微服务架构的影响。 一、RPC的强类型检查 RPC的强类型检查是其核心优势之一。在RPC通信中,客户端和服务端都使

el-upload 上传图片及回显照片和预览图片,文件流和http线上链接格式操作

<div v-for="(info, index) in zsjzqwhxqList.helicopterTourInfoList" :key="info.id" >编辑上传图片// oss返回线上地址http链接格式:<el-form-itemlabel="巡视结果照片":label-width="formLabelWidth"><el-upload:action="'http:

HTTP状态码中301与302的区别

一.官方说法  301,302 都是HTTP状态的编码,都代表着某个URL发生了转移,不同之处在于:  301 redirect: 301 代表永久性转移(Permanently Moved)。  302 redirect: 302 代表暂时性转移(Temporarily Moved )。  这是很官方的说法,那么它们的区别到底是什么呢?  1.1、什么是301转向?什么是301重定向?

物联网系统运维——移动电商应用发布,Tomcat应用服务器,实验CentOS 7安装JDK与Tomcat,配置Tomcat Web管理界面

一.Tomcat应用服务器 1.Tomcat介绍 Tomcat是- -个免费的开源的Ser Ivet容器,它是Apache基金会的Jakarta 项目中的一个核心项目,由Apache, Sun和其他一 些公司及个人共同开发而成。Tomcat是一一个小型的轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP程序的首选。 在Tomcat中,应用程序的成部署很简

jmeter测试https请求

公司最近在搞全站HTTPS改造,进一步提高网站的安全性,防止运营商劫持。那么,改造完成后,所有前后端的URL将全部为https。 So ,研究下怎么用Jmeter访问https请求呢。 其实很简单, 第一步在jmeter中创建HTTP请求,如下图进行配置,https端口为443; 第二步,在本机浏览器,如Chrome中导入该域名证书,在更多工具-设置-管理证书的地方,找到该证书,导出到本地。然后在

关于IE get 请求报400

问题描述: 在使用IE8进行get请求时,参数中有中文存在,发现发送请求之后,返回http状态码400 问题解决方法: 把请求连接进行处理window.encodeURI('http://aaa:8080/wtp?name=小明'); window.location.href=window.encodeURI('http://aaa:8080/wtp?name=小明'); 然后在把处理后

删除第三方AAR所请求的权限

1.问题:引了第三方的AAR库要求一些权限,但我APP不需要,我想删除掉,怎么办 答:很简单,只要在添加:http://schemas.android.com/tools 这个工具在manifest文件中,然后在相应的权限上增加:tools:node="remove"即可。 <manifest xmlns:android="http://schemas.android.com/apk/res/a

使用 axios 进行 HTTP 请求

使用 axios 进行 HTTP 请求 文章目录 使用 axios 进行 HTTP 请求1、介绍2、安装和引入3、axios 基本使用4、axios 发送 GET 请求5、axios 发送 POST 请求6、高级使用7、总结 1、介绍 什么是 axios axios 是一个基于 promise 的 HTTP 库,可以用于浏览器和 Node.js 中发送 HTT

HTTP基本概念介绍

HTTP概述 HTTP : 超文本传输协议,HTTP是浏览器端Web通信的基础。 一, 两种架构 B/S架构:Browser/Server,浏览器/服务器架构。 B:  浏览器,比如Firefox 、Google 、Internet; S:  服务器,Apache,nginx; C/S架构:Client/Server,客户端/服务器架构。 B/S架构相对于C/S架构,客户机上无需安装任何软件