Posting data and HTAnchor_findAddress

Hi --

I've spent days trying to get a post request to work.  If someone could 
take a look at the code below, I'd be extremely grateful.  All I want to 
do (since I'm passing in username and password) is to send the 
name-value pairs as a POST rather than a GET.  The arguments that I'm 
passing in are as follows:

"https://localhost:4567/esmih/CCIServlet" "jobType=register" 
"eName=test" "userName=BigBen" "password=London"

In the code below, uri and nameValuePairs have the following values:
        uri = https://localhost:4567/esmih/CCIServlet
        nameValuePairs = 
jobType=register+eName=test+userName=BigBen+password=London+

When I debug, the HTAnchor_findAddress function creates new anchor 
because one isn't found in the hash table.  It populates it with the 
default values.  The default values never change so the I'm not sure if 
this is causing a problem....suggestions?  Please?

Thanks,
Juliana
 
*********************************************************************
#include "WWWLib.h"
#include "WWWHTTP.h"
#include "WWWInit.h"
#include "WWWSSL.h"
#include <string.h>
#include <stdlib.h>

#define APP_NAME        "configUtility"
#define APP_VERSION        "1.0"
#define MAX_BUFFER        512


PRIVATE int terminate_handler (HTRequest * request, HTResponse * response,
                   void * param, int status)
{
    HTEventList_stopLoop ();
    return HT_ERROR;
}
void usage()
{
   printf("Enter the URI of the management station you want to POST to 
and the contents to POST.\n");
   printf("\t%s <URI> <POST body contents>\n", APP_NAME);
   printf("EXAMPLE: %s <protocol://host[:port]/URI> <param1>=<value1> 
<param2>=<value2>...\n", APP_NAME);
}

PUBLIC HTChunk* HTPostAnchorToChunk(HTParentAnchor* src, HTAnchor* dest, 
HTRequest* req)
{
    if (src && dest && req)
    {
        HTChunk* chunk = NULL;
        HTStream* target = HTStreamToChunk(req, &chunk, 0);
        HTRequest_setOutputStream(req, target);
        if (NULL != HTPostAnchor(src, dest, req))
        {
            return chunk;
        }
        else
        {
            HTChunk_delete(chunk);
            return NULL;
        }
    }
    return NULL;
}

int main (int argc, char ** argv)
{

   HTRequest* request = NULL;
   HTParentAnchor* source = NULL;
   HTAnchor* uriObject = NULL;
   HTChunk* chunkMain = NULL;
   char* uri = NULL;
   char* nameValuePairs = NULL;
   char* pwd = NULL;
   BOOL status = NO;

   HTLibInit(APP_NAME, APP_VERSION);
 
   HTProfile_newNoCacheClient(APP_NAME, APP_VERSION);

   HTNet_addAfter(terminate_handler, NULL, NULL, HT_ALL, HT_FILTER_LAST);

   if(argc >= 3)
   {
    int index = 0;
    printf("Number of args = %d\n", argc);
   
    uri = (char *)malloc(strlen(argv[1]) + 1);
    if (NULL == uri)
    {
        return -1;
    }
    strcpy(uri, argv[1]);
    printf("URI = %s\n", uri);
   
    nameValuePairs = (char *)malloc(MAX_BUFFER + 1);
    if (NULL == nameValuePairs)
    {
        return -1;
    }
  
    printf("nameValuePairs var should be empty now.  Is it?  %s\n", 
nameValuePairs);
    for (index = 2; index < argc; index++)
    {
        printf("argv[%d] = %s\n", argv[index]);
       
        /*  Need to still handle special cases (i.e. space) and encode 
them  */
        nameValuePairs = strcat(nameValuePairs, argv[index]);
        printf("name-value pair #%d added: %s\n", index, nameValuePairs);
        nameValuePairs = strcat(nameValuePairs, "+"); 
    }  
   }
   else
   {
    usage();exit(1);
   
   }

    HTSSL_protMethod_set(HTSSL_V23);
    HTSSL_verifyDepth_set(2);
    HTSSLhttps_init(NO);
    HTTransportInit();
    HTProtocolInit();
    HTNetInit();
   
    if (nameValuePairs && *nameValuePairs && uri && *uri)
    { 
    request = HTRequest_new();
   
    uriObject = HTAnchor_findAddress(uri);
   
    source = HTTmpAnchor(NULL);
    HTAnchor_setDocument(source, nameValuePairs);
    HTAnchor_setFormat(source, WWW_PLAINTEXT);

    HTAnchor_setLength(source, strlen(nameValuePairs));

    chunkMain = HTPostAnchorToChunk(source, uriObject, request);

    if (chunkMain)
    {
        char * string;
        HTEventList_loop (request);
        string = HTChunk_toCString(chunkMain);
        printf("%s", string ? string : "No response from server\n");
        HT_FREE(string);
    }
   
    free(uri);
    free(nameValuePairs);
   
    HTRequest_delete(request);
    HTFormat_deleteAll();
   
    HTSSLhttps_terminate();
    HTLibTerminate(); 
    }
   
    return 0;
}

Received on Monday, 7 April 2003 14:59:18 UTC