void CUrlTestDlg::HttpPost(CString strUrl,CString strRequest)
{
CString strServer;
CString strObjectName;
CInternetSession m_Session(_T("HttpClient"));
CHttpConnection * m_Server = NULL;
CHttpFile * m_File = NULL;
INTERNET_PORT nPort = 0;
DWORD dwServerType = 0;
if(AfxParseURL(strUrl,dwServerType,strServer,strObjectName,nPort))
{
CString strHeader =_T("Content-Type: application/x-www-form-urlencoded\r\n");
strHeader += _T("User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:9.0.1) Gecko/20100101 Firefox/9.0.1\r\n");
try
{
m_Server = m_Session.GetHttpConnection(strServer,nPort);
m_File = m_Server->OpenRequest(CHttpConnection::HTTP_VERB_POST,strObjectName,NULL,1,(LPCSTR*)"Accept:*/*","HTTP/1.1");
m_File->AddRequestHeaders(_T("Accept-Language: zh-cn"));
m_File->AddRequestHeaders(_T("Accept-Charset:UTF-8\r\n"));
m_File->SendRequest(strHeader,(LPVOID)(LPCTSTR)strRequest,strRequest.GetLength());
DWORD httpStatus;
m_File->QueryInfoStatusCode(httpStatus);
if (httpStatus == HTTP_STATUS_OK)
{
CString strLine;
CString strInfo;
while(m_File->ReadString(strLine) > 0)
strInfo += strLine;
if(strInfo.IsEmpty())
{
::AfxMessageBox(_T("添加成功!"));
}
else
{
::AfxMessageBox(strInfo);
}
}
else
{
::AfxMessageBox(_T("添加失败!"));
}
}
catch (CInternetException* e)
{
e->ReportError();
e->Delete();
}
if(m_File)
delete m_File;
if (m_Server)
delete m_Server;
m_Session.Close();
}
}