Solved

MFC Application that post data to a CGI.

Posted on 1998-04-14
3
515 Views
Last Modified: 2013-11-20
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:

DWORD dwHttpRequestFlags = INTERNET_FLAG_EXISTING_CONNECT |
INTERNET_FLAG_NO_AUTO_REDIRECT;
DWORD   dwAccessType = PRE_CONFIG_INTERNET_ACCESS;
const TCHAR szHeaders[] = _T("Accept: text/plain\r\nUser-Agent: POST
Tester\r\n");

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

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

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

The script is named formmail.pl, 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/test.pl
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.
0
Comment
Question by:skidz
3 Comments
 
LVL 12

Expert Comment

by:Otta
ID: 1317741
0
 
LVL 3

Expert Comment

by:altena
ID: 1317742
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.
0
 
LVL 11

Accepted Solution

by:
mikeblas earned 200 total points
ID: 1317743
// 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__;
#endif

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;
   }
   else
   {
      CInternetSession sess("YourAgent");

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

      CString strPost(
         "email=spaceape@nowhere.net&"
         "email_required=Your+e-mail+address+is+required+to+continue.&"
         "brand=BridgePath");
 
      pFile->SendRequest(strHeaders, (LPVOID)(LPCTSTR) strPost, strPost.GetLength());

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

      pFile->Close();
      delete pFile;
      pConnection->Close();
   }

   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

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are two main kinds of selectors in CSS: One is base selector like h1, h2, body, table or any existing HTML tags.  For instance, the following rule sets all paragraphs (<p> elements) to red: (CODE) CSS also allows us to define our own custom …
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question