Embedded Tools
  Home arrow Embedded Tools arrow Page 3 - HTTP Requesting with eVC++ 3.0 on Windows ...
IBM developerWorks
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Actuate Whitepapers 
Moblin 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
EMBEDDED TOOLS

HTTP Requesting with eVC++ 3.0 on Windows CE 3.0
By: Ben Shepherd
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 13
    2003-05-20

    Table of Contents:
  • HTTP Requesting with eVC++ 3.0 on Windows CE 3.0
  • Protocols and Packets
  • Creating your HTTP Request
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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:

    • InternetOpen
    • InternetConnect
    • InternetCrackUrl
    • HttpOpenRequest
    • HttpSendRequest
    • InternetReadFile
    • InternetCloseHandle

    InternetOpen

    This function has the following prototype.

    HANDLE InternetOpen(LPCTSTR lpszAgent,
                                  DWORD dwAccessType,
                                  LPCTSTR lpszProxy,
                                  LPCTSTR lpszProxyByPass,
                                  DWORD dwFlags);

    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.

    BOOL InternetCrackUrl(LPCTSTR lpszUrl,
                                   DWORD dwUrlLength,
                                   DWORD dwFlags,
                                   LPURL_COMPONENTS lpUrlComponents);

    To initialise the crackedURL variable, it is good practise to call the function memset to set each element of the variable to 0.

    URL_COMPONENTS crackedUrl;
    memset(&crackedUrl, 0, sizeof(crackedUrl));

    Now the variable is initialised, let's implement it using the following variables,

    • dwStructSize
    • lpszHostName
    • dwHostNameLength
    • lpszUrlPath
    • dwUrlPathLength
    TCHAR szServer[1024];
    TCHAR szPath[1024];
    crackedUrl.dwStructSize = sizeof(crackedUrl);
    crackedUrl.lpszHostName = szServer;
    crackedUrl.dwHostNameLength = 1024;
    crackedUrl.lpszUrlPath = szPath;
    crackedUrl.dwUrlPathLength = 1024;


    Note: In the code below szUrl is the URL.

    Say, www.devarticles.com/mynewpda/winCE/index.asp.

    InternetCrackUrl(szUrl, 0, 0, &crackedUrl);

    The result is lpszHostName would be valued at www.devarticles.com, while lpszUrlPath would be valued at /mynewpda/winCE/index.asp.

    InternetConnect

    When the programmer wishes to connect to a server. This function has the following prototype.

    HANDLE InternetConnect(HINTERNET hInternet,
                                      LPCTSTR lpszServerName,
                                      INTERNET_PORT nServerPort,
                                      LPCTSTR lpszUserName,
                                      LPCTSTR lpszPassword,
                                      DWORD dwService,
                                      DWORD dwFlags,
                                      DWORD dwContext);

    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.

    HANDLE HttpOpenRequest(HINTERNET hHttpSession,
                                        LPCTSTR lpszVerb,
                                        LPCTSTR lpszObjectName,
                                        LPCTSTR lpszVersion,
                                        LPCTSTR lpszReferrer,
                                        LPCTSTR *lplpszAcceptTypes,
                                        DWORD dwFlags,
                                        DWORD dwContext);

    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.

    BOOL HttpSendRequest(HINTERNET hRequest,
                                     LPCTSTR lpszHeaders,
                                     DWORD dwHeadersLength,
                                     LPVOID lpOptional,
                                     DWORD dwOptionalLength);

    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.

    BOOL InternetReadFile(HINTERNET hFile,
                                   LPVOID lpBuffer,
                                   DWORD dwNumberOfBytesToRead,
                                   LPDWORD lpdwNumberOfBytesRead);

    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. 

    #define CHUNKSIZE 201

    Here is some code to get you on the right track.

    char charBuffer[CHUNKSIZE];
    TCHAR szBuffer[CHUNKSIZE];
    DWORD dwRead;
    do
    {
          if(!InternetReadFile(hHttpRequest, 
                                 charBuffer,
                                 CHUNKSIZE - 1,
                                 &dwRead))
          {
                cout << _T("ERROR: ") << GetLastError();
                break;
          }
          charBuffer[dwRead] = '\0';
          mbstowcs(szBuffer, charBurred, dwRead);
          szBuffer[dwRead] = '\0';
          cout << szBuffer;
    } while(dwRead > 0);

    Note: the function mbstowcs is used to convert a multibyte ANSI string to a wide character UNICODE string.

    Now the request has been successful, it is now time to close the HTTP handle.

    InternetCloseHandle

    This function must be called so that all of the handles to be returned from their use in the Internet functions.

    Note: InternetCloseHandle is called opposed the the CloseHandle function because it is not a kernel object. This function has the following prototype.

    BOOL InternetCloseHandle(HINTERNET hInternet);

    This simply suggests that the function is used to close a handle. The function may be implemented as follows.

    if(hHttpRequest != NULL)
    {
          InternetCloseHandle(hHttpRequest);
    }
    if(hHttpOpen != NULL)
    {
          InternetCloseHandle(hHttpOpen);
    }

    Note: With the HANDLE type functions, the return value on failure is NULL. For the BOOL type functions, the return value on failure is FALSE.

    More Embedded Tools Articles
    More By Ben Shepherd


     

    EMBEDDED TOOLS ARTICLES

    - A New Toolbar Can Boost Your Web Site Traffi...
    - Programming Custom Hardware for Windows
    - Developing Embedded Systems - A Tools Introd...
    - WAP for Fun and Profit - Part 1 - Basic WML ...
    - HTTP Requesting with eVC++ 3.0 on Windows CE...


    Iron Speed





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway