//调用方法
BOOL bRecv = HttpSend(TEXT("255,255,255,255"),TEXT("导航"),TEXT("内容"),TRUE);
void DisplayStat(LPSTR lBuffer)
{
OutputDebugStringA(lBuffer);
OutputDebugStringA("\n");
}
/////////////////////////////////以下为函数源码
BOOL HttpSend(CString strSever, CString Object,CString strValue,BOOL bIsAuto)
{
CHttpConnection* m_Connection = NULL;
CHttpFile* m_File = NULL;
CString m_Ret;//用来接收返回信息
CInternetSession m_Session(AfxGetAppName (),0,INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
TRY
{
m_Session.SetOption (INTERNET_OPTION_CONNECT_RETRIES,1);
m_Session.SetOption (INTERNET_OPTION_CONNECT_TIMEOUT,20000);
m_Session.SetOption (INTERNET_OPTION_RECEIVE_TIMEOUT,20000);
INTERNET_PORT nPort = 80;
CString strServer = strSever;
m_Connection = m_Session.GetHttpConnection(strServer,nPort,NULL,NULL );
if (m_Connection == NULL)
{
DisplayStat ("An error occurred connecting to the server");
Sleep (2000);
goto end;
}
LPCTSTR ppszAcceptTypes[2];
ppszAcceptTypes[0] = _T("*/*");
ppszAcceptTypes[1] = NULL;
CString strObject = Object;
strObject = Object + strValue;
m_File = m_Connection->OpenRequest(NULL, strObject, NULL,1,ppszAcceptTypes, NULL, INTERNET_FLAG_RELOAD |
INTERNET_FLAG_DONT_CACHE);
if (m_File == NULL)
{
DisplayStat ("打开服务器上的文件出错");
goto end;
}
if (!m_File->SendRequest (NULL,0,NULL,0))
{
DisplayStat ("An error occurred connecting to the server");
Sleep (2000);
goto end;
}
TCHAR szStatusCode[32];
DWORD dwInfoSize = 32;
if (m_File->QueryInfo (HTTP_QUERY_STATUS_CODE,szStatusCode,&dwInfoSize))
{
long nStatusCode = _ttol(szStatusCode);
if (nStatusCode == HTTP_STATUS_REQUEST_TIMEOUT)
{
DisplayStat ("连接超时,请重试");
goto end;
}
else if (nStatusCode != HTTP_STATUS_OK)
{
DisplayStat("服务器端错误");
goto end;
}
//状态正确,开始读取信息
}
else
{
DisplayStat("无法取得服务器返回信息");
goto end;
}
char szReadBuf[1024];
DWORD dwBytesToRead = 1024;
DWORD m_ActualSize = 0;
do
{
ZeroMemory (szReadBuf,1024);
DWORD m_ActualSize = m_File->Read (szReadBuf,dwBytesToRead);
m_Ret = m_Ret + CString(szReadBuf);
}while(!(m_ActualSize < dwBytesToRead));
TRACE("%s",m_Ret);
int iFirst = m_Ret.Find (TEXT("requert:"),0);
CString strTemp = m_Ret.Left(iFirst+sizeof(TEXT("requert:"))/2+1);
int iLast = strTemp.Find (TEXT("requert:"),0);
strTemp = strTemp.Left (iLast+sizeof(TEXT("requert:"))/2+1);
if (strTemp == "requert:1") //操作成功
{
DisplayStat("数据上传成功");
if (m_File)
{
m_File->Close ();
delete m_File;
}
if (m_Connection)
{
m_Connection->Close ();
delete m_Connection;
}
m_Session.Close ();
DisplayStat ("空闲。。。");
return TRUE;
}
else if(strTemp == "requert:0") //操作失败
{
DisplayStat("操作失败");
if (m_File)
{
m_File->Close ();
delete m_File;
}
if (m_Connection)
{
m_Connection->Close ();
delete m_Connection;
}
m_Session.Close ();
DisplayStat ("空闲。。。");
return FALSE;
}
else
{
DisplayStat("数据上传失败,请稍候重试");
goto end;
}
}
CATCH (CInternetException, e)
{
TCHAR errStr[100];
e->GetErrorMessage (errStr,100);
OutputDebugString(errStr);
OutputDebugString(TEXT("\n"));
if (m_File)
{
m_File->Close ();
delete m_File;
m_File = NULL;
}
if (m_Connection)
{
m_Connection->Close ();
delete m_Connection;
m_Connection = NULL;
}
m_Session.Close ();
DisplayStat ("空闲。。。");
return FALSE;
}
END_CATCH
end:
if (m_File)
{
m_File->Close ();
delete m_File;
m_File = NULL;
}
if (m_Connection)
{
m_Connection->Close ();
delete m_Connection;
m_Connection = NULL;
}
m_Session.Close ();
DisplayStat ("空闲。。。");
return FALSE;
}