CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 1999
    Location
    CA
    Posts
    188

    post to a CGI. How do I ...

    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?



  2. #2
    Join Date
    May 1999
    Location
    South Africa
    Posts
    46

    Re: post to a CGI. How do I ...

    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.


  3. #3
    Join Date
    May 1999
    Location
    CA
    Posts
    188

    Re: post to a CGI. How do I ...

    Thanks. This should come in handy.



  4. #4
    Guest

    Re: post to a CGI. How do I ...

    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



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured