How to use CCHttpClient


Induction

CCHttpClient is an interface for HTTP client. HTTP clients encapsulate a smorgasbord of objects required to execute HTTP requests while handling cookies, authentication, connection management, and other features. Thread safety of HTTP clients depends on the implementation and configuration of the specific client.

Concepts

The general usage of CCHttpClient consists of just 6 steps:

  • Create an instance of CCHttpClient.

  • Create an instance of one of the methods (setUrl for example). The URL to connect to is passed in to the the method constructor.

  • Tell CCHttpClient to execute the method.

  • Read the response.

  • Release the connection.

  • Deal with the response.

How To Use

Instantiating HttpClient

The no argument constructor for CCHttpClient provides a good set of defaults for most situations so that is what we’ll use.

cocos2d::extension::CCHttpRequest* request = new cocos2d::extension::CCHttpRequest();

Creating a url

The various methods defined by the HTTP specification correspond to the various classes in CCHttpClient.
We will be using the Get method which is a simple method that simply takes a URL and gets the document the URL points to.

request->setUrl("http://www.httpbin.org/get");

GET

The following is an example an HTTP Get request via CCHttpClient.

request->setRequestType(cocos2d::extension::CCHttpRequest::kHttpGet);
request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted));
request->setTag("GET test1");
cocos2d::extension::CCHttpClient::getInstance()->send(request);
request->release();

POST

The following will send a post request to the URL “http://www.httpbin.org/post” .

cocos2d::extension::CCHttpRequest* request = new cocos2d::extension::CCHttpRequest();
request->setUrl("http://www.httpbin.org/post");
request->setRequestType(cocos2d::extension::CCHttpRequest::kHttpPost);
request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted));

// write the post data
const char* postData = "visitor=cocos2d&TestSuite=Extensions Test/NetworkTest";
request->setRequestData(postData, strlen(postData));

request->setTag("POST test1");
cocos2d::extension::CCHttpClient::getInstance()->send(request);
request->release();

Read the Response

It is vital that the response body is always read regardless of the status returned by the server. There are three ways to do this:
Call getRequestData() This will return a raw data containing the data in the response body.

 /** Get the request data pointer back */
inline char* getRequestData()

Release the Connection

This is a crucial step to keep things flowing. We must tell CCHttpClient that we are done with the connection and that it can now be reused. Without doing this CCHttpClient will wait indefinitely for a connection to free up so that it can be reused.

request->release();

Deal with the Response

We’ve now completed our interaction with CCHttpClient and can just concentrate on doing what we need to do with the data. In our case, we’ll just print it out to the console.

// dump data
std::vector *buffer = response->getResponseData();
printf("Http Test, dump data: ");
for (unsigned int i = 0; i < buffer->size(); i++)
{
    printf("%c", (*buffer)[i]);
}
printf("\n");

It’s worth noting that if you were retrieving the response as a stream and processing it as it is read, this step would actually be combined with reading the connection, and when you’d finished processing all the data, you’d then close the input stream and release the connection.

Android

Note that if you environment is Android do not forget adding permissions on your app’s Manifest:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值