MFC Application that post data to a CGI.

CHttpFile::SendRequest and it's siblings (AddRequestHeaders, OpenRequest) are not behaving.  I am trying to POST to a CGI script FORMMAIL from inside of an MFC program (so users could update statistics, place orders, etc) and am
getting all sorts of failures.  Here is what I am trying to do:

const TCHAR szHeaders[] = _T("Accept: text/plain\r\nUser-Agent: POST

if (!AfxParseURL(host, dwServiceType, strServerName, strObject, nPort) ||
    m_OutPut += "!AfxParseUrl";

   try {
    pServer = session.GetHttpConnection(strServerName, nPort);
   catch (CInternetException* pEx) {
    pServer = NULL;

   pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST, strObject,
NULL, 1, NULL, NULL, dwHttpRequestFlags);
   pFile->SendRequest(NULL, (ULONG )0, (void *)(LPTSTR)(LPCTSTR)data,

The script is named, and takes two inputs user and message (inside
of the script I break a part the messages)

I tried to do the assignments like this:

host = http://eudoxus/cgi-bin/
data = ?name=matt&message=This+is+a+test

But it's not working.

Anyone can come out with a source-code can earn my 200 points. I've written some sample code. You just modify from it. Or you can just write you own simple MFC programme to do a POST using any public cgi program. Thanks.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sorry, I dont give away code...

If you want to I can point you to some articles in the
Microsoft knowdelge base that adres the "posting from an
application" issue.
// compile me for debug with:
//       cl /Zi /MTd doit.cpp
// compile me for release with:
//      cl /MT doit.cpp

#include <afx.h>
#include <afxwin.h>
#include <afxinet.h>
#include <iostream>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;

CWinApp theApp;

using namespace std;

CString strHeaders =
      "Content-Type: application/x-www-form-urlencoded\r\n\r\n";

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
   int nRetCode = 0;

   //REVIEW: this app does no error-checking whatsoever

   if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
      cerr << _T("Fatal Error: MFC initialization failed") << endl;
      nRetCode = 1;
      CInternetSession sess("YourAgent");

      CHttpConnection* pConnection = sess.GetHttpConnection("");
      CHttpFile *pFile = NULL;
      pFile = pConnection->OpenRequest("POST", "/jobseek/inputinitial.cfm");

      CString strPost(
      pFile->SendRequest(strHeaders, (LPVOID)(LPCTSTR) strPost, strPost.GetLength());

      CString str;
      while (pFile->ReadString(str))
         cout << (LPCTSTR) str;
         cout << endl;

      delete pFile;

   return nRetCode;

The thing that's wrong with your code, certainly, is that your "data" contains a leading question mark ('?'). It's not necessary for you to supply the question mark; it's up to WININET.DLL to do that.

Since you ask a very vague question (eg, "It's not working"), and we don't know exactly what the server-side component is expecting to receive, my answer is a real shot in the dark. But the most likely problem is that you didn't include a Content-Type: header on your request, and the server is summarily rejecting your request because it's the wrong content type.

Another likely problem is that you don't have all the required parameters; maybe you're not providing values for hidden fields, for instance, or setting other headers that your server-side software requires.

.B ekiM


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.