Solved

MFC Application that post data to a CGI.

Posted on 1998-04-14
3
527 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 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

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

617 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