Maverick
Maverick

Reputation: 1187

CHTTPFile::SendRequest get feedback data from server

I used successfully to post a form to my webserver using MFC's function CHttpFile::SendRequest. The server respond success with return code 200.

The problem is that I want to get more data from the server like a custom process code or a custom string code. For example pFile->ReadString(str) gives me the chance to read something but what ?

How to setup my server to send back to caller such informations ? My server runs apache.

Upvotes: 0

Views: 1328

Answers (1)

Maverick
Maverick

Reputation: 1187

The Read/ReadString below returns anything you want to return. Just decode each line you receive from the server.

pFile->ReadString(str)

CString MyClass::PostWebFormActivation() {

CString             strHeaders;
CInternetSession    session;
CString             strFormData;
CHttpConnection     *pConnection;
CHttpFile           *pFile;
BOOL                result;
DWORD               dwRet;
CString             strServerResponse;
CString             strExceptionError;
INTERNET_PORT       nPort(80);
CString             strServer;
CString             strObject;
CString             strSubmitValue = _T("1");
CString             strLine;
CString             strTemp;
INT                 pos;
CUtils              util;


// URL-encoded form variables -

// serial = "xxxxxxx", systemcode = "xxxxx", username = "xxxxxxx", restoreemail = "xxxxxxx". strCAPTCHA = "Xyz!"
strFormData = _T(SUBMIT_FORM) + _T(strSubmitValue)+ _T(SERIAL_FIELD) + _T(m_strSerial) + _T(SYSTEMCODE_FIELD) + _T(m_strSystemCode) + _T(USERNAME_FIELD) + _T(m_strUsername) + _T(RESTORE_EMAIL_FIELD) + _T(m_strRestoreEmail) + _T(CAPTCHA_FIELD);

pConnection = NULL;
pFile = NULL;

//I can use the AfxParseURL instead of the following code
strServer = ((CMainFrame *)AfxGetMainWnd())->m_strWebActivateProductURL;
strTemp = strServer;
strTemp.MakeUpper();
pos = strTemp.Find(_T("HTTP://"));
if (pos != -1) {

    strServer = strServer.Mid(7); // remove http://

}

pos = strServer.Find('/',0);

strObject = strServer.Mid(pos); //the path after the domain ie /dir/index.php
strServer = strServer.Left(pos);//server is the actual domain ie www.mywebsite.gr


if (util.CheckURLFileExtension(strObject) == _T("") ) {
    //folder
    if (strObject.Find('/',strObject.GetLength()) == -1) {
        strObject += _T("/?");
    }

} else {

    //file
    strObject += _T("?");

}




try {

    pConnection = session.GetHttpConnection( strServer , nPort);


    pFile = pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST /*HTTP_VERB_GET*/,strObject /*_T("/paradox4a/activate/?") or _T("/paradox4a/activate/index.php?")*/);

    //pFile->AddRequestHeaders(szHeaders); //not needed.
    strHeaders = _T("Content-Type: application/x-www-form-urlencoded\r\n"); 

    result = pFile->SendRequest(strHeaders, (LPVOID) (LPCTSTR) strFormData, strFormData.GetLength());

    if (result > 0) {

        pFile->QueryInfoStatusCode(dwRet);
        strServerResponse.Empty();
        if (dwRet == HTTP_STATUS_OK) {
            while(pFile->ReadString(strLine)) {
                strServerResponse += strLine + "\r\n";
            }

        } else {        
            strServerResponse = _T("ERROR");
            AMLOGINFO(_T("Trying to post a form to server for activation procedure, got communication error [%d]" ), dwRet);
        }

    } else {
        strServerResponse = _T("ERROR");
        AMLOGINFO(_T("Trying to post a form to server for activation procedure, got communication error [%d]" ), result );
    }


} catch (CInternetException* pEx)   {
    TCHAR sz[1024];

    CString s = util.getInetError(pEx->m_dwError);
    strServerResponse = _T("ERROR");
    pEx->GetErrorMessage( sz,1024 );
    pEx->Delete();

    AMLOGINFO(_T("Trying to post a form to server for activation procedure, got network error [%s]" ), strExceptionError );

}


if (pFile) {
    pFile->Close();
    delete pFile;
}

if (pConnection) {
    pConnection->Close();
    delete pConnection;
}





return strServerResponse;

}

Upvotes: 1

Related Questions