CHTTPFile::SendRequest 从服务器获取反馈数据

CHTTPFile::SendRequest get feedback data from server

我使用 MFC 的函数 CHttpFile::SendRequest 成功地 post 向我的网络服务器发送了一个表单。服务器用 return 代码 200 响应成功。

问题是我想从服务器获取更多数据,例如自定义进程代码或自定义字符串代码。例如 pFile->ReadString(str) 让我有机会阅读一些东西,但是什么?

如何设置我的服务器以将此类信息发回给呼叫者?我的服务器运行 apache。

下面的Read/ReadStringreturn随便你return。只需解码您从服务器收到的每一行。

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;

}