
// Post a Request //
#include <w3c-libwww/WWWLib.h>
#include <w3c-libwww/WWWInit.h>
PRIVATE HTChunk * result = NULL;
PRIVATE int printer (const char * fmt, va_list pArgs)
{
    return (vfprintf(stdout, fmt, pArgs));
}
PRIVATE int tracer (const char * fmt, va_list pArgs)
{
    return (vfprintf(stderr, fmt, pArgs));
}
PRIVATE int terminate_handler (HTRequest * request, HTResponse * response, void * param, int status) 
{
    HTPrint("Status=%d\n",status);
    if (status == HT_LOADED || result || HTChunk_data(result)) 
    {
	char *data;
	FILE *fp;
	int i;
	data=HTChunk_data(result);
	fp=fopen("Login_Page.txt","w");
	for(i=0;data[i]!='\0';i++)
		fputc(data[i],fp);
     }
    HTChunk_delete(result);
    HTRequest_delete(request);
    HTProfile_delete();
    exit(0);
}
int main (int argc, char ** argv)
{
    HTRequest * request = NULL;
    HTAnchor * anchor = NULL;
    HTAssocList * formfields = NULL;
    char * uri = NULL;
    HTProfile_newNoCacheClient("TestApp", "1.3");
    HTPrint_setCallback(printer);
    HTTrace_setCallback(tracer);
    /* Get trace messages */
#if 0
    HTSetTraceMessageMask("sop");
#endif
    HTNet_addAfter(terminate_handler, NULL, NULL, HT_ALL, HT_FILTER_LAST);
    HTHost_setEventTimeout(100000);
    if (argc >= 2) 
    {
	int arg;
	uri = argv[1];
	for (arg=2; arg<argc; arg++) 
       {
	    char * string = argv[arg];
	    if (!formfields) formfields = HTAssocList_new();
	    HTParseFormInput(formfields, string);
	}
    }
    if (uri && formfields) 
    {
	request = HTRequest_new();
	HTRequest_setOutputFormat(request, WWW_RAW);
	anchor = HTAnchor_findAddress(uri);
	result = HTPostFormAnchorToChunk(formfields, anchor, request);
	HTAssocList_delete(formfields);
	HTEventList_loop(request);
    } else {
	HTPrint("Type the URI to post to and the arguments for the POST operation. Encode spaces as '+'\n");
	HTPrint("\t%s <uri> 'a=1' 'b=+2+' 'c=3 ...'\n", argv[0]);
    }
    return 0;
}
