Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MFC Application that post data to a CGI.

Posted on 1998-04-14
3
Medium Priority
?
533 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 800 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

Independent Software Vendors: 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

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

722 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