2

I use CInternetSession with GetHttpConnection, but I can't find anywhere a good info on how to fill and post a web form.

sehe
  • 374,641
  • 47
  • 450
  • 633
Mario
  • 13,941
  • 20
  • 54
  • 110

1 Answers1

3

This knowledge base article explains how to simulate a POST request using CInternetSession.

The included sample code goes like this:

CString strHeaders = _T("Content-Type: application/x-www-form-urlencoded");
// URL-encoded form variables -
// name = "John Doe", userid = "hithere", other = "P&Q"
CString strFormData = _T("name=John+Doe&userid=hithere&other=P%26Q");

CInternetSession session;
CHttpConnection *pConnection = session.GetHttpConnection(_T("ServerNameHere"));
CHttpFile *pFile = pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST,
    _T("FormActionHere"));
BOOL result = pFile->SendRequest(strHeaders, (LPVOID) (LPCTSTR) strFormData,
    strFormData.GetLength());
Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • 1
    And how can I read the reponse page which is sent back by web server? – Mario Dec 22 '11 at 03:41
  • It will be available in `pFile`. You can use its `Read()` and `ReadString()` methods to extract it. – Frédéric Hamidi Dec 22 '11 at 08:47
  • After posting I get return code 200 which is ok, and then if I read the pFile I get a missing page result – Mario Dec 22 '11 at 14:48
  • Do you mean you get IIS's standard `404` error page with a `200` status code? [This can happen](http://stackoverflow.com/q/4996874/464709) in some circumstances. Are you perchance submitting your request to an ASP.NET page or handler? Is the web server under your control? – Frédéric Hamidi Dec 22 '11 at 15:04
  • Hi, Yes I get the IIS 404 error page with the 200 status code. The web page is an ASP.NET 4 page and yes the server is under my control. – Mario Dec 22 '11 at 15:33
  • Then it would be interesting to know if this behavior is the result of a call to `TransferRequest()` (potentially meaning it's a legitimate response to your query) or if it comes from an unhandled exception of some kind. The `Application` event log on the server should contain entries about those, you might want to give it a look. – Frédéric Hamidi Dec 22 '11 at 15:39
  • Hi, There is no entry in the event viewer application log, I don't have a transferrequest yet, I have a label on the form page to display the received content for debugging until I get this working. – Mario Dec 22 '11 at 17:30
  • Since you're posting to a page, maybe that's a view state problem. Can you update your question with the server-side code of your page, and the client C++ code you wrote from this answer? We might be able to help further then. – Frédéric Hamidi Dec 22 '11 at 17:55