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
Solved

MFC Application that post data to a CGI.

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

828 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