C sample

Here's a client example written in the ubiquitous C language. It uses a free HTTP library available from http://www.kayfamily.demon.co.uk/x3/projects/; I don't believe in re-inventing the wheel. Again, no parsing is performed; it reads the request from one file and writes the response to another. Finding a public-domain DOM parser for C is left as an exercise for the reader.

#include	<stdio.h>
#include	<string.h>
#include	<errno.h>
#include	<fcntl.h>
#include	<sys/types.h>
#include	<sys/stat.h>
#include	<http.h>

char	*pgmName;

main( int argc, char **argv ) {
	HTTP_CONNECTION	*conn;
	struct stat	statBuf;
	char		*reqBuf;
	FILE		*fp;
	int		fd;
	int		rc;

	/*
	 * get program name and ensure proper usage
	 */

	if( ( pgmName = strrchr( *argv, '/' ) ) == NULL )
		pgmName = *argv;
	else
		pgmName++;
	argc--;
	argv++;

	if( argc != 2 ) {
		fprintf( stderr, "Usage: %s filein fileout\n", pgmName );
		exit( 12 );
	}

	/*
	 * get the stat struct associated with the file (it contains
	 * the file length) and open the file
	 */

	if( stat( *argv, &statBuf ) < 0 ) {
		fprintf( stderr, "%s: stat(%s): %s\n", pgmName, *argv,
		  strerror( errno ) );
		exit( 12 );
	}

	if( ( fd = open( *argv, O_RDONLY ) ) < 0 ) {
		fprintf( stderr, "%s: open(%s): %s\n", pgmName, *argv,
		  strerror( errno ) );
		exit( 12 );
	}

	/*
	 * allocate a character buffer and read in the entire file
	 */

	if( ( reqBuf = (char *) malloc( statBuf.st_size + 1 ) ) == NULL ) {
		fprintf( stderr, "%s: malloc(): %s\n", pgmName,
		  strerror( errno ) );
		exit( 12 );
	}

	rc = read( fd, reqBuf, statBuf.st_size );
	if( rc < 0 ) {
		fprintf( stderr, "%s: read(%s): %s\n", pgmName, *argv,
		  strerror( errno ) );
		exit( 12 );
	}

	if( rc != statBuf.st_size ) {
		fprintf( stderr, "%s: read(%s): wanted %d, got %d\n", pgmName,
		  *argv, statBuf.st_size, rc );
		exit( 12 );
	}

	reqBuf[statBuf.st_size] = '\0';
	close( fd );
	argv++;

	/*
	 * open the output file
	 */

	if( ( fp = fopen( *argv, "w" ) ) == NULL ) {
		fprintf( stderr, "%s: fopen(%s): %s\n", pgmName, *argv,
		  strerror( errno ) );
		exit( 12 );
	}

	/*
	 * create the connection, connect, POST and disconnect
	 */

	if( ( conn = http_connection_create( "localhost", 80 ) ) == NULL ) {
		fprintf( stderr, "%s: http_connection_create(): failed\n",
		  pgmName );
		exit( 12 );
	}

	if( ( rc = http_connection_connect( conn ) ) < 0 ) {
		fprintf( stderr, "%s: http_connection_connect(): failed\n",
		  pgmName );
		exit( 12 );
	}

	if( ( rc = http_post( conn, "/servlet/demo", reqBuf, fp ) ) < 0 ) {
		fprintf( stderr, "%s: http_post(): failed\n", pgmName );
		exit( 12 );
	}

	http_connection_disconnect( conn );

	/*
	 * close output file and terminate
	 */

	fclose( fp );
	exit( 0 );
}

Apologies for all the error trapping code, but when programming in C I'd rather be safe than sorry. In Java you can just put all the code inside a try block and catch any exception thrown. That's only one of many advantages of the Java language.

Copyright © 2000 by Phil Selby
All rights reserved internationally.