Developers Manual > Cocos2d-x > Modules > Network > How to use CCHttpClient

How to use HttpClient

Induction

HttpClient 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 HttpClient consists of just 6 steps:

  • Create an instance of HttpClient.

  • 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 HttpClient to execute the method.

  • Read the response.

  • Release the connection.

  • Deal with the response.

How To Use

Instantiating HttpClient

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

cocos2d::network::HttpRequest* request = new cocos2d::network::HttpRequest();

Creating a url

The various methods defined by the HTTP specification correspond to the various classes in HttpClient.
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 HttpClient.

request->setRequestType(cocos2d::network::HttpRequest::Type::GET);
request->setResponseCallback( CC_CALLBACK_2(HttpClientTest::onHttpRequestCompleted, this) );
request->setTag("GET test1");
cocos2d::network::HttpClient::getInstance()->send(request);
request->release();

POST

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

cocos2d::network::HttpRequest* request = new cocos2d::network::HttpRequest();
request->setUrl("http://www.httpbin.org/post");
request->setRequestType(cocos2d::network::HttpRequest::Type::POST);
request->setResponseCallback( CC_CALLBACK_2(HttpClientTest::onHttpRequestCompleted, this) );

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

request->setTag("POST test1");
cocos2d::network::HttpClient::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 getResponseData() This will return a raw data containing the data in the response body.

 /** Get the request data pointer back */
inline std::vector* getResponseData()

Release the Connection

This is a crucial step to keep things flowing. We must tell HttpClient that we are done with the connection and that it can now be reused. Without doing this HttpClient 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 HttpClient 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 :
<uses-permission android:name="android.permission.INTERNET"/>

Sign up for our newsletter to keep up with the latest developments, releases and updates for Cocos2d-x.