【问题标题】:CHTTPFile::SendRequest get feedback data from serverCHTTPFile::SendRequest 从服务器获取反馈数据
【发布时间】:2016-03-13 09:37:13
【问题描述】:

我曾经使用 MFC 的函数 CHttpFile::SendRequest 成功地将表单发布到我的网络服务器。服务器响应成功,返回码 200。

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

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

【问题讨论】:

    标签: post visual-c++ mfc http-post wininet


    【解决方案1】:

    下面的 Read/ReadString 返回您想要返回的任何内容。只需解码从服务器收到的每一行。

    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;
    

    }

    【讨论】:

    • 嗨特立独行。你能告诉我如何发送 POST 消息然后获取服务器响应吗?请给 flaviu2@yahoo.com 写一封电子邮件……我不知道你的电子邮件……我真的需要这个例子!
    • 查看上面的例子。缺少一些类,例如 CUtils,但了解每个语句的作用并不是什么大问题。
    猜你喜欢
    • 1970-01-01
    • 2013-12-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-27
    • 1970-01-01
    • 1970-01-01
    • 2021-10-21
    • 2017-11-20
    相关资源
    最近更新 更多