Web client
[Apps]
Detailed Description
This example shows a HTTP client that is able to download web pages and files from web servers. It requires a number of callback functions to be implemented by the module that utilizes the code: webclient_datahandler(), webclient_connected(), webclient_timedout(), webclient_aborted(), webclient_closed().
Define Documentation
#define UIP_APPCALL webclient_appcall |
#define WEBCLIENT_CONF_MAX_URLLEN 100 |
#define WEBCLIENT_STATE_CLOSE 3 |
#define WEBCLIENT_STATE_DATA 2 |
#define WEBCLIENT_STATE_HEADERS 1 |
#define WEBCLIENT_STATE_STATUSLINE 0 |
#define WEBCLIENT_TIMEOUT 100 |
Typedef Documentation
Function Documentation
void webclient_aborted |
( |
void |
|
) |
|
Callback function that is called from the webclient code if the HTTP connection to the web server has been aborted by the web server.
This function must be implemented by the module that uses the webclient code.
void webclient_appcall |
( |
void |
|
) |
|
void webclient_close |
( |
void |
|
) |
|
Close the currently open HTTP connection.
void webclient_closed |
( |
void |
|
) |
|
Callback function that is called from the webclient code when the HTTP connection to the web server has been closed.
This function must be implemented by the module that uses the webclient code.
void webclient_connected |
( |
void |
|
) |
|
Callback function that is called from the webclient code when the HTTP connection has been connected to the web server.
This function must be implemented by the module that uses the webclient code.
void webclient_datahandler |
( |
char * |
data, |
|
|
u16_t |
len | |
|
) |
| | |
Callback function that is called from the webclient code when HTTP data has been received.
This function must be implemented by the module that uses the webclient code. The function is called from the webclient module when HTTP data has been received. The function is not called when HTTP headers are received, only for the actual data.
- Note:
- This function is called many times, repetedly, when data is being received, and not once when all data has been received.
- Parameters:
-
| data | A pointer to the data that has been received. |
| len | The length of the data that has been received. |
char * webclient_filename |
( |
void |
|
) |
|
Obtain the filename of the current HTTP data stream.
The filename of an HTTP request may be changed by the web server, and may therefore not be the same as when the original GET request was made with webclient_get(). This function is used for obtaining the current filename.
- Returns:
- A pointer to the current filename.
unsigned char webclient_get |
( |
char * |
host, |
|
|
u16_t |
port, |
|
|
char * |
file | |
|
) |
| | |
Open an HTTP connection to a web server and ask for a file using the GET method.
This function opens an HTTP connection to the specified web server and requests the specified file using the GET method. When the HTTP connection has been connected, the webclient_connected() callback function is called and when the HTTP data arrives the webclient_datahandler() callback function is called.
The callback function webclient_timedout() is called if the web server could not be contacted, and the webclient_aborted() callback function is called if the HTTP connection is aborted by the web server.
When the HTTP request has been completed and the HTTP connection is closed, the webclient_closed() callback function will be called.
- Note:
- If the function is passed a host name, it must already be in the resolver cache in order for the function to connect to the web server. It is therefore up to the calling module to implement the resolver calls and the signal handler used for reporting a resolv query answer.
- Parameters:
-
| host | A pointer to a string containing either a host name or a numerical IP address in dotted decimal notation (e.g., 192.168.23.1). |
| port | The port number to which to connect, in host byte order. |
| file | A pointer to the name of the file to get. |
- Return values:
-
| 0 | if the host name could not be found in the cache, or if a TCP connection could not be created. |
| 1 | if the connection was initiated. |
char * webclient_hostname |
( |
void |
|
) |
|
Obtain the hostname of the current HTTP data stream.
The hostname of the web server of an HTTP request may be changed by the web server, and may therefore not be the same as when the original GET request was made with webclient_get(). This function is used for obtaining the current hostname.
- Returns:
- A pointer to the current hostname.
void webclient_init |
( |
void |
|
) |
|
Initialize the webclient module.
char * webclient_mimetype |
( |
void |
|
) |
|
Obtain the MIME type of the current HTTP data stream.
- Returns:
- A pointer to a string contaning the MIME type. The string may be empty if no MIME type was reported by the web server.
unsigned short webclient_port |
( |
void |
|
) |
|
Obtain the port number of the current HTTP data stream.
The port number of an HTTP request may be changed by the web server, and may therefore not be the same as when the original GET request was made with webclient_get(). This function is used for obtaining the current port number.
- Returns:
- The port number of the current HTTP data stream, in host byte order.
void webclient_timedout |
( |
void |
|
) |
|
Callback function that is called from the webclient code if the HTTP connection to the web server has timed out.
This function must be implemented by the module that uses the webclient code.