HTTP Requesting with eVC++ 3.0 on Windows CE 3.0 - Creating your HTTP Request (Page 3 of 4 )
HTTP uses sockets for communicating, as well as specifying the content and formatting details of the data being transferred. Now it is time to create the HTTP Handler. Let’s view the system we are working with.
To create the trivial HTTP program we need to use the following functions:
Note: the fourth parameter LPCTSTR lpszProxyByPass should be set to NULL in Windows CE 3.0 because it is not supported.
The function is
called once, usually at the moment when the application begins or when the first HTTP request is made. This function is used to initialise Windows CE Internet functions located in the wininet.h and wininet.lib.
This function when called will create a handle to give access to this functions. Once the Window CE Internet functions have completed their tasks, the HTTP handle must be closed.
Initially, the handle will be set to NULL.
HINTERNET hHttpOpen = NULL;
When it is required, you'd call the InternetOpen function.
hHttpOpen = InternetOpen(_T("Application Id"), // Application identification INTERNET_OPEN_TYPE_DIRECT, // No proxy server access NULL, // No name for proxy server NULL, // No bypass addresses 0); // No flags - Typical
This now initialises the HTTP handler for the proxy-less application.
InternetCrackUrl
This function is disigned to retrieve information contained in the URL and has the following prototype.
If your server do not require a username and password, simply set the fourth and fifth parameters to NULL values. Let's implement this InternetConnect function. Firstly, create a connection object hHttpSession.
HINTERNET hHttpSession = NULL;
Now, the programmer should connect to the server using the handle that was created using the InternetOpen function as well as the Server name retieved via the crackedUrl function.
hHttpSession = InternetConnect(hHttpOpen, // Handle crackedURL.lpszHostName, // Server name INTERNET_DEFAULT_HTTP_PORT,// HTTP Port number NULL, // No username NULL, // No password INTERNET_SERVICE_HTTP, // Service or protocol 0, // No flags 0); // No context
Now there is a connection the the server, let's create a HTTP Request handle.
HttpOpenRequest
This function is Http protocol specific and has the following prototype.
Obtaining a Http Request handle can be done in the following way.
HINTERNET hHttpRequest = NULL; hHttpRequest = HttpOpenRequest(httpSession, // Handle from InternetConnect NULL, // HTTP verb is 'GET' crackedUrl.lpszPath,// CrackedUrl path NULL, // Default version is 'HTTP/1.0' NULL, // No referrer NULL, // Only 'text/*' files are accepted 0, // No flags 0); // No context
Once we have a HTTP handle we can make a request. This is done by calling the HttpSendRequest function.
HttpSendRequest
This function is called when the programmer wishes to make a HTTP request. The function has the following prototype.
So, to send a request to the server the programmer should type the following code,
HttpSendRequest(hHttpRequest, // Using the handle that was just created NULL, // No additional headers 0, // No length required 0, // No optional data 0); // No length required
Now the HTTP request is sent, it is time to retrieve the data.
InternetReadFile
This function retrieves the data returned from the HTTP request. The function has the following prototype.
This function is called repeatly. Each time the function is called a chunk of data, either binary or text, is read into a buffer. Let make the chunksize 201, where the extra element represents the null character.