Rumah > Soal Jawab > teks badan
UINT CHTTPDownload::SendRequest(BOOL bHead)
{
CString strVerb;
if( bHead ) strVerb = _T("HEAD ");
else strVerb = _T("GET ");
CString strSend,strHeader,strRange;
int iStatus = 0, nRet;
char szReadBuf[1025];
DWORD dwContentLength,dwStatusCode;
while (TRUE)
{
if(m_pSocket.m_hSocket != NULL)
m_pSocket.Close();
m_pSocket.Create();
m_pSocket.Connect(m_strServer, m_nPort);
strSend = strVerb + m_strObject + " HTTP/1.1\r\n";
strSend += "Host: " + m_strServer + "\r\n";
strSend += "Accept: */*\r\n";
strSend += "Pragma: no-cache\r\n";
strSend += "Cache-Control: no-cache\r\n";
if( !m_strReferer.IsEmpty() )
strSend += "Referer: " + m_strReferer + "\r\n";
strSend += "Connection: close\r\n";
strRange = "Range: bytes=100-\r\n";
strSend += strRange;
strSend += "\r\n";//必须要加一个空行,否则Http服务器将不会应答
int ret = m_pSocket.Send(strSend.GetBuffer(0), strSend.GetLength());
strSend.ReleaseBuffer();
strHeader.Empty();
while( TRUE )
{
ZeroMemory(szReadBuf,1025);
ret = m_pSocket.Receive(szReadBuf, 1025);
if( szReadBuf[0] == '\0' ) // We have encountered "\r\n\r\n"
break;
strHeader += szReadBuf;
if( iStatus == 0)
strHeader += "\r\n";
}
nRet = GetInfo(strHeader,dwContentLength,dwStatusCode,m_TimeLastModified);
switch ( nRet )
{
case HTTP_FAIL:
return SENDREQUEST_FAIL;
break;
case HTTP_ERROR:
return SENDREQUEST_ERROR;
break;
case HTTP_REDIRECT:
continue;
break;
case HTTP_OK:
m_dwDownloadSize = dwContentLength + 100;
if( strRange.IsEmpty() )
m_dwFileSize = dwContentLength + 100; // 整个文件的长度
else
{
if ( dwStatusCode == 206 ) //支持断点续传
{
m_bSupportResume = TRUE;
m_dwFileSize = dwContentLength + 100;
}
else //不支持断点续传
{
m_bSupportResume = FALSE;
m_dwFileSize = dwContentLength + 100;
}
}
return SENDREQUEST_SUCCESS;
break;
default:
return SENDREQUEST_FAIL;
break;
}
}
m_pSocket.Close();
return SENDREQUEST_SUCCESS;
}