1、函数调用
CString URL ="http://ip:8080/XXXX/Register";
CString para = "username=&password=123456";
CString result;
PostContent(URL, para, result);
2、头文件(调用函数实现)post.h
#include "afxinet.h"
#include <string>
bool PostContent(CString strUrl, const CString&strPara, CString &strContent)
{
try{
boolbRet = false;
CStringstrServer, strObject, strHeader, strRet;
unsignedshort nPort;
DWORDdwServiceType;
if(!AfxParseURL(strUrl, dwServiceType, strServer, strObject, nPort))
{
returnfalse;
}
CInternetSessionsess;//Create session
CHttpFile*pFile;
CHttpConnection*pServer = sess.GetHttpConnection(strServer, nPort);
if(pServer == NULL)
{
returnfalse;
}
pFile= pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST, strObject, NULL, 1, NULL,NULL, INTERNET_FLAG_EXISTING_CONNECT);
if(pFile == NULL)
{
returnfalse;
}
CStringstrHeaders = "Content-Type:application/x-www-form-urlencoded;";
pFile->SendRequest(strHeaders,(LPTSTR)(LPCTSTR)strPara, strPara.GetLength());
AfxMessageBox((LPTSTR)(LPCTSTR)strPara)
CStringstrSentence;
DWORDdwStatus;
DWORDdwBuffLen = sizeof(dwStatus);
BOOLbSuccess = pFile->QueryInfo(
HTTP_QUERY_STATUS_CODE| HTTP_QUERY_FLAG_NUMBER,
&dwStatus,&dwBuffLen);
if(bSuccess && dwStatus >= 200 && dwStatus<300)
{
charbuffer[256];
memset(buffer,0, 256);
intnReadCount = 0;
while((nReadCount = pFile->Read(buffer, 2048)) > 0)
{
strContent+= buffer;
memset(buffer,0, 256);
}
bRet= true;
}
else
{
bRet= false;
}
pFile->Close();
sess.Close();
returnbRet;
}
catch(...)
{
intnCode = GetLastError();
returnfalse;
}
}