Simple Client Sample

The simple HTTP server sample demonstrates how to add HTTP client functionality to your C application. The AppWeb HTTP client allows applications to retrieve documents using GET and POST HTTP method calls. The sample is a main program that requests the home page from http://www.mbedthis.com.

Files

Makefile
simpleClient.cpp

Makefile

The Makefile will build on Windows or Linux. A Windows VS 6.0 project file is also supplied.

Typical output from the Makefile build is listed below. This is the output on a Windows system:

cl -o simpleClient.exe simpleClient.cpp -Zi -Od -D_NDEBUG -W3 -nologo -MDd -FD -DWIN -D_DLL -D_MT \
  -D_WINDOWS -DWIN32 -D_WIN32_WINNT=0x500 -D_X86_=1 -D_CRT_SECURE_NO_DEPRECATE -D_USRDLL \
  -I../../../include  ../../../bin/libappWebStatic.lib ws2_32.lib advapi32.lib user32.lib

Source Code

simpleClient.cpp

///
/// @file   simpleClient.cpp
/// @brief  Simple client using the GET method to retrieve a web page.
///
/// This sample demonstrates retrieving content using the HTTP GET 
/// method via the Client class. This is a multi-threaded application.
///
//  Copyright (c) Mbedthis Software LLC, 2003-2005. All Rights Reserved.
//
////////////////////////////// Includes ////////////////////////////////

#include    "appWeb/appWeb.h"

////////////////////////////////// Code ////////////////////////////////

int main(int argc, char** argv)
{
    MaClient    *client;
    Mpr         mpr("simpleClient");
    char        *content;
    int         code, contentLen;

    //
    //  Start the Mbedthis Portable Runtime
    //
    mpr.start(MPR_SERVICE_THREAD);

    //
    //  Get a client object to work with. We can issue multiple 
    //  requests with this one object.
    //
    client = new MaClient();

    //
    //  Get a URL
    //
    if (client->getRequest("http://www.mbedthis.com/index.html") < 0) {
        mprFprintf(MPR_STDERR, "Can't get URL");
        exit(2);
    }

    //
    //  Examine the HTTP response HTTP code. 200 is success.
    //
    code = client->getResponseCode();
    if (code != 200) {
        mprFprintf(MPR_STDERR, "Server responded with code %d\n", code);
        exit(1);
    } 

    //
    //  Get the actual response content
    //
    content = client->getResponseContent(&contentLen);
    if (content) {
        mprPrintf("Server responded with:\n");
        write(1, content, contentLen);
    }

    delete client;
    return 0;
}



© Mbedthis Software LLC, 2003-2005. All rights reserved. Mbedthis is a trademark of Mbedthis Software LLC.