Cum pot trimite unui server variabile POST ?
Raspuns
Folosind clasele MFC CInternetSession, CHttpConnection si CHttpFile se creeaza o sesiune/conexiune de internet, iar apoi cu ajutorul clasei CHttpFile se trimit variabilele POST prin CHttpFile::SendRequest(...) .
Exemplu
Code: Select all
class CTestWinInetDoc : public CDocument
{
// Operations
public:
CString SendRequest(CString sServer, CString sFile, CString sFormData);
}
Code: Select all
CString CTestWinInetDoc::SendRequest(CString sServer, CString sFile, CString sFormData)
{
CString sRet;
CString sHeaders = _T("Content-Type: application/x-www-form-urlencoded");
CInternetSession Session;
CHttpFile* pFile = NULL;
CHttpConnection* pConnection = NULL;
try
{
pConnection = Session.GetHttpConnection(sServer);
pFile = pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST,sFile);
pFile->SendRequest(sHeaders,(LPVOID)(LPCTSTR)sFormData,sFormData.GetLength());
}
catch(CInternetException* pInternetException)
{
pInternetException->GetErrorMessage(sRet.GetBuffer(255),255);
sRet.ReleaseBuffer();
pInternetException->Delete();
}
if(pConnection)delete pConnection;
if(pFile)delete pFile;
return sRet;
}
Code: Select all
void CTestWinInetView::OnHelpSendrequest()
{
CString sRet = GetDocument()->SendRequest(_T("www.codexpert.ro"),_T("index.php"),
_T("page=contact&send=1&name=popescu&message=generic%message"));
if(! sRet.IsEmpty())SetWindowText(sRet);
else SetWindowText(_T("Your form data was sent successfully"));
}
<< Back to MFC index