Click to See Complete Forum and Search --> : post to a CGI. How do I ...


Eric Smith
July 15th, 1999, 01:13 PM
Hi.

I need to post data to a cgi.
I create a socket using CSocket::Create(); That works fine.
I connect to the machine usint CSocket::Connect("www.address.com",80); That works fine.

Now I am left with either making a CSocketFile or using CSocket::Send(); The problem is that the cgi I need to send to is not in the root directory of the host. IE it is at "www.address.com/somedir/somedir-cgi/here.cgi". I have been raking the CSocket information of VC6 for anything, but it looks like everything has to go to the root. Can anyone shed light on this problem?

DanielF
July 16th, 1999, 05:20 AM
I'd suggest using CInternetSession, CHttpConnection and CHttpFile to get to the server; its easier than just using sockets.

Here is some code you can look at to see how to do it.



CInternetSession session("My Session");
CHttpConnection* pServer = NULL;
CHttpFile* pFile = NULL;
CString ServerName = "www.address.com";
INTERNET_PORT nPort = 80;
DWORD retcode;

char outBuff[300] = "?something=data+over+here&somethingelse=more+data";

try
{
pServer = session.GetHttpConnection(ServerName,nPort);
pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST,"/cgi-bin/somecgi.cgi",NULL,1,NULL,NULL,INTERNET_FLAG_EXISTING_CONNECT);
pFile -> AddRequestHeaders("Content-Type: application/x-www-form-urlencoded");
pFile -> AddRequestHeaders("Accept: */*");
pFile -> SendRequest(NULL,0,outBuff,strlen(outBuff)+1);

pFile -> QueryInfoStatusCode(retcode);
// you can read from the file after this......I've just left it out.
}
catch (CInternetException * e);

delete pFile;
delete pServer;
session.Close();





I haven't put any comments in because I've done this quickly for you....and haven't tested *this* code....but it does work.....if you have any questions about this...or it doesn't work : ( please feel free to write and ask.

Dan.

Eric Smith
July 16th, 1999, 02:22 PM
Thanks. This should come in handy.

September 15th, 1999, 09:24 PM
oops!

char outBuff[300]="?something=data+over+here&somethingelse=more+data";

should be:
char outBuff[300]="something=data+over+here&somethingelse=more+data";
without '?'.

Good Luck