#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);

#define httpresponse_selector(_SELECTOR) (cocos2d::extension::SEL_HttpResponse)(&_SELECTOR)

/**

@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)

{

_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;

};

/** 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)

{

_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 */

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的更多相关文章

  1. .net学习笔记----HttpRequest,WebRequest,HttpWebRequest区别

    WebRequest是一个虚类/基类,HttpWebRequest是WebRequest的具体实现 HttpRequest类的对象用于服务器端,获取客户端传来的请求的信息,包括HTTP报文传送过来的所 ...

  2. 防刷票机制研究和.NET HttpRequest Proxy

    最近应朋友之约 测试他做的投票网站 防刷票机制能力如何,下面有一些心得和体会. 朋友网站用PHP写的,走的是HttpRequest,他一开始认为IP认证应该就差不多了.但说实话这种很low,手动更换代 ...

  3. python httprequest, locust

    r = self.client.get("/orders", headers = {"Cookie": self.get_user_cookie(user[0] ...

  4. c# WebBrower 与 HttpRequest配合 抓取数据

    今天研究一个功能,发现一个问题. 通过webbrower模拟用户自动登录可以完成,并且可以取到相对应的页面内容. 但是如果页面中通过ajax,动态加载的内容,这种方式是取不到的,于是用到了httpRe ...

  5. Asp.net中HttpRequest.Params与Reques.Item之异同

    今天才注意到HttpRequest.Params与HttpRequest.Item这两个玩意竟然有微妙的不同.上午的时候同事被坑了发现这玩意的说明还真微妙. 场景再现: 前台提交一个POST请求到后台 ...

  6. HttpRequest重写,解决资源战胜/链接超时/分块下载事件通知 问题。

    /************************************************************************************** 文 件 名: WebRe ...

  7. 对象化的Http和请求对象HttpRequest

    在面向对象的语言中,有种“万物皆对象”的说法.在上篇文章中介绍了HttpRuntime类,在该类收到请求之后,立即通过HttpWorkerRequest工作者对象对传递的参数进行分析和分解,创建方便网 ...

  8. ASP.Net核心对象HttpRequest

    描述context. Request["username"]; 通过这种方式,能够得到一个HttpRequest对象.HttpRequest对象描述了,关于请求的相关信息,我们可以 ...

  9. .net学习笔记----HttpRequest类

    一.HttpRequest的作用 HttpRequest的作用是令到Asp.net能够读取客户端发送HTTP值.比如表单.URL.Cookie传递过来的参数. 返回字符串的那些值就不说了,那些基本上都 ...

  10. Win7下 httpRequest带证书请求https网站

    常规情况下创建Web请求,并获取请求数据的代码如下: WebRequest req = WebRequest.Create(url); req.Timeout = 15000; WebResponse ...

随机推荐

  1. PHP经验集锦

    最近刚刚完成手中的项目,比较闲.来这儿转转,把积累的一些技巧分享给大家!1.关于PHP重定向 方法一:header("Location: index.php"); 方法二:echo ...

  2. wifi详解(二)

    1        Wifi模块解析和启动流程 1.1      框架分析 WIFI整体框架如图所示: 首先,用户程序使用WifiManager类来管理Wifi模块,它能够获得Wifi模块的状态,配置和 ...

  3. java web 学习十四(JSP原理)

    一.什么是JSP? JSP全称是Java Server Pages,它和servle技术一样,都是SUN公司定义的一种用于开发动态web资源的技术. JSP这门技术的最大的特点在于,写jsp就像在写h ...

  4. linux命令——磁盘管理pwd

    Linux中用 pwd 命令来查看”当前工作目录“的完整路径(绝对路径). 简单得说,每当你在终端进行操作时,你都会有一个当前工作目录.在不太确定当前位置时,就会使用pwd来判定当前目录在文件系统内的 ...

  5. WCF扩展

    WCF 可扩展性 WCF 提供了许多扩展点供开发人员自定义运行时行为. WCF 在 Channel Layer 之上还提供了一个高级运行时,主要是针对应用程序开发人员.在 WCF 文档中,它常被称为服 ...

  6. lighttpd mod_status模块

    用过nginx的status可以查看服务器的状态,之后就想lighttpd有没有这样的模块呢 之后看下配置文件,真的有,然后就试下 第一步, "mod_auth" 把这个前面的#号 ...

  7. oc_转_构造对象的方法,以及类的继承

    一.构造方法 (一)构造方法的调用 完整的创建一个可用的对象:Person *p=[Person new]; New方法的内部会分别调用两个方法来完成2件事情: 1) 使用alloc方法来分配存储空间 ...

  8. SVM原理简介

    本文只是简单介绍一下SVM的理论框架,想要详细了解当中细节问题处理可以参看后续章节或者网上各种详细资料.推荐Andrew Ng的斯坦福大学机器学习课程. 年代中期发展起来的基于统计学习理论的一种机器学 ...

  9. 关于C++ vector的拷贝

    定义了vector变量,在使用时直接用了等号赋值,后来发现有问题,就查了一下vector怎么赋值? 说明:vector是一个构造对象,不能直接使用=符号进行复制,必须迭代每个元素来复制.或者重载=操作 ...

  10. 第二百四十三天 how can I 坚持

    制定的计划完成不了了,好多问题啊.又想当然了,晚上加了会班. 今天雾霾好严重,一出地铁大裤衩怎么没了.雾霾爆表啊. 还好现在刮大风了. 准备看<芈mi月传>了. 睡觉.