#include <HttpURLConnection.h>
A sub-class of URLConnection with support for HTTP-specific features, mainly used to make a http request and process the response.
This base class provides some functions including socket connection, issuing of requests and managing responses. This base class also provides numbers of helper functions help set or get http request and response header fields. However, only most commonly used http request and response header fields are covered. Developers who want to set or get all http header fields can do it themselves through functions like setRequestField(), addRequestField() and getResponseField(wchar_t *) / getResponseField(int).
For technical specification of http header fields, visit web site HTTP header fields.
Remarks Making a http request by HttpURLConnection
Making a http request is the main function of class URLConnection. The following steps are the typical steps of making a http request by HttpURLConnection, notice that some steps and settings are required while others are optional.
Remarks Making an http request by WebUtil::sendHttpRequestAsync
WebUtil class provide a helper function WebUtil::sendHttpRequestAsync() that make it more convenient to issue a http request.
This helper function encapsulates the typical steps shown in Making a http request by HttpURLConnection, it also ease off the job by put the whole operation into a AsyncTask so that the current thread will not be blocked when issuing the request and waiting the response.
Although function WebUtil::sendHttpRequestAsync() has less control options than using HttpURLConnection class, its can cover most common scenarios when sending http request and the source code can be very compact.
Classes | |
class | EventListener |
class | ExceptionListener |
Static Public Member Functions | |
static HttpURLConnection * | openConnection (URL *url, void *userDefined=0) |
![]() | |
static wchar_t * | getExceptionMessage (int exceptionType) |
Static Public Attributes | |
static const wchar_t * | METHOD_GET |
static const wchar_t * | METHOD_POST |
static const wchar_t * | CONTENT_TYPE_FORM_URLENCODED |
static const wchar_t * | CONTENT_TYPE_FORM_MULTIPART |
static const wchar_t * | CONTENT_TYPE_TEXT_PLAIN |
static const wchar_t * | HEADER_CONTENT_ACCEPT |
static const wchar_t * | HEADER_USER_AGENT |
static const wchar_t * | HEADER_CONTENT_TYPE |
static const wchar_t * | HEADER_CONTENT_LENGTH |
static const wchar_t * | HEADER_SET_COOKIE |
![]() | |
static const int | DEFAULT_READ_BUFFER_SIZE = 2048 |
Additional Inherited Members | |
![]() | |
void * | userDefined |
![]() | |
URLConnection (URL *url, void *userDefined) | |
enum REQUEST_METHOD |
Values that represent http request methods.
enum ENCODE_TYPE |
Values that represent encoding types.
Enumerator | |
---|---|
ENCODE_TYPE_NONE | Indicates no encoding. |
ENCODE_TYPE_URLENCODED | The URL encoding |
ENCODE_TYPE_MULTIPART | The multi-part content-type, works with POST method, mainly used to upload files to the server. |
ENCODE_TYPE_PLAINTEXT | The plain text. |
enum HTTP_STATUS_CODE |
Values that represent HTTP status codes.
enum HTTP_EXCEPTION_TYPE |
Values that represent exceptions may be raised by HttpURLConnection class
|
virtual |
Destructor.
|
static |
A helper function that opens a url connection and returns a HttpURLConnection instance for issuing http request and processing the http response.
url | The URL |
userDefined | The general-purpose parameter that carries user-defined information, usually used to distinguish the context under which the connection is created and used. |
NULL
if fails. void setRequestMethod | ( | wchar_t * | method | ) |
Sets the http request method.
Note The current version of Pooka SDK® only supports http request methods METHOD_GET and METHOD_POST.
method | The http request method, must be either METHOD_GET or METHOD_POST. |
wchar_t* getRequestMethod | ( | ) |
Gets the http request method.
Note The current version of Pooka SDK® only supports http request methods METHOD_GET and METHOD_POST.
void writeRequestBody | ( | char * | requestBody, |
int | bodySize | ||
) |
Writes the http request body.
Since the current version of Pooka SDK® only supports http request methods METHOD_GET and METHOD_POST, this function is mainly used to send post data to the http server.
requestBody | The http request body. |
bodySize | Size of the http request body in bytes |
void readFullResponse | ( | HttpURLConnection::ExceptionListener * | exceptionListener = 0 , |
HttpURLConnection::EventListener * | eventListener = 0 , |
||
int | readBufferSize = URLConnection::DEFAULT_READ_BUFFER_SIZE |
||
) |
Issue the http request and starts receiving server response.
There is no "dedicated" function like "sendRequest()" to issue a http request in class HttpURLConnection. Issuing the http request and processing the response are done by a single call to this function. If the request is sent by POST method, the the post data need to be sent separately by function writeRequestBody(). See Making a http request by HttpURLConnection for more details.
Note This function blocks the current caller thread until the response reading is fully terminated. For modern application, this function should be called on a worker thread to avoid blocking the UI thread.
exceptionListener | The exception listener used to handle http exceptions, see ExceptionListener for details. |
eventListener | The event listener used to handle various http events, see EventListener for details. |
readBufferSize | The size of the read buffer that determines the size of the data chunk can be read at one time, see function EventListener::onHttpData() for more details. |
int getResponseCode | ( | ) |
Gets the HTTP status codes returned by the HTTP server
wchar_t* getResponseMessage | ( | ) |
Gets the http response message returned by the HTTP server
void addRequestField | ( | wchar_t * | field, |
wchar_t * | value | ||
) |
Adds the given field to the http request header. Existing fields with the same name will not be overwritten by this function.
field | The field name |
value | The field value |
void setRequestField | ( | wchar_t * | field, |
wchar_t * | value | ||
) |
Sets the value of the specified http request header field. If a field with the same name already exists, overwrite its value with the given value.
field | The field name |
value | The field value |
wchar_t* getRequestField | ( | wchar_t * | field | ) |
Gets the value of the specified http request header field.
field | The field name |
NULL
if there is no such field with the specified name Core::MiniSTL::DataStructure::Properties* getRequestFields | ( | ) |
Gets all http request fields.
Note All field names in the returned Properties are in uppercase.
void setRequestContentType | ( | wchar_t * | value | ) |
A helper function that sets the http request header field Content-Type
with given value.
value | The field value |
void setRequestContentLength | ( | int | value | ) |
A helper function that sets the http request header field Content-Length
with given value.
This function is used when sending http POSt data to the server. Liek the following sample code:
value | The field value |
void setRequestUserAgent | ( | wchar_t * | value | ) |
A helper function that sets the http request header field User-Agent
with given value.
Note By default, Pooka SDK® application sets the user agent to Pooka SDK®
. If other agent name is preferred, developers can use this function to change it before submit the http request.
value | The field value |
void setRequestAccept | ( | wchar_t * | value | ) |
A helper function that sets the http request header field Accept
with given value.
value | The field value |
wchar_t* getResponseField | ( | int | index | ) |
Gets the value of http response header field at the specified position.
index | Zero-base position of the field in http response header |
NULL
if the http response header has fields fewer than the argument index
specifies. wchar_t* getResponseField | ( | wchar_t * | field | ) |
Gets the value of http response header field specified by the field name
field | The field name |
NULL
if the http response header does not have such field. wchar_t* getResponseFieldName | ( | int | index | ) |
Gets the name of http response header field at the specified position.
index | Zero-base position of the field in http response header |
NULL
if the http response header has fields fewer than the argument index
specifies. Core::MiniSTL::DataStructure::Properties* getResponseFields | ( | ) |
Gets all http response header fields
Note All field names in the returned Properties are in lowercase.
wchar_t* getResponseContentType | ( | ) |
A helper function that retrieves the value of http response header field Content-Type
.
This function returns the MIME-type of the content specified by the http response header field "Content-Type", or NULL
is returned if type is not specified.
NULL
id fails. int getResponseContentLength | ( | ) |
A helper function that retrieves the value of http response header field Content-Length
.
Content-Length
field is not set or cannot be represented as an int. wchar_t* getResponseContentEncoding | ( | ) |
A helper function that retrieves the value of http response header field Content-Encoding
.
NULL
id fails. long long getResponseDate | ( | ) |
A helper function that retrieves the value of http response header field Date
in milliseconds since January 1, 1970 GMT, or 0
if not specified.
NULL
id fails. long long getResponseExpiration | ( | ) |
A helper function that retrieves the value of http response header field Expires
in milliseconds since January 1, 1970 GMT, or 0
if not specified.
NULL
id fails. long long getResponseLastModified | ( | ) |
A helper function that retrieves the value of http response header field Last-Modified
in milliseconds since January 1, 1970 GMT, or 0
if not specified.
NULL
id fails. Core::MiniSTL::DataStructure::ArrayList* getResponseCookies | ( | ) |
Gets all cookies in http response header.
This function returns all the value strings of the "Set-Cookie" fields in http response header by a ArrayList .
Caution The returned ArrayList instance and all its element should be treated as read-only and should not be destroyed.
|
static |
The string constant of http request method GET, the constant value is GET
.
|
static |
The string constant of http request method POST, the constant value is POST
.
|
static |
One of content types for html form. This constant has value application/x-www-form-urlencoded
.
This is the default content type. Html forms submitted with this content type must be encoded as follows:
|
static |
One of content types for html form. This constant has value multipart/form-data
.
When html forms needs to send large quantities of binary data or text containing non-ASCII characters, the content type multipart/form-data
should be used. It is efficient for submitting forms that contain files, non-ASCII data, and binary data.
|
static |
One of content types indicates plain text content. This constant has value text/plain
.
|
static |
One of http header fields that indicates content types that are acceptable for the response. This constant has value Accept
.
|
static |
One of http header fields that contains the identifier string of the user agent. This constant has value User-Agent
.
|
static |
One of http header fields that indicates the MIME type of this content. This constant has value Content-Type
.
|
static |
One of http header fields that indicates the length of the request body in bytes. This constant has value Content-Length
.
|
static |
One of http header fields that indicates a http cookie. This constant has value Set-Cookie
.