//获取http服务器上要下载文件的大小
ULONGLONG GetHttpFileSize( LPCTSTR szUrl)
{
CString strUrl = szUrl;
strUrl.Replace(_T("//"),_T("/"));
CInternetSession Session;
CHttpFile *pHttpFile = NULL;
CString strServerName;
CString strObject; // 查询对象名(http文件)
INTERNET_PORT nPort; // 端口
DWORD dwServiceType; // 服务类型
DWORD dwHttpRequestFlags = INTERNET_FLAG_NO_AUTO_REDIRECT | INTERNET_FLAG_RELOAD | INTERNET_FLAG_DONT_CACHE;
const TCHAR szHeaders[]=_T("Accept: */* User-Agent:HttpClient ");
CHttpConnection* pServer = NULL;
//网址分析
BOOL bOK = AfxParseURL(strUrl, dwServiceType, strServerName, strObject, nPort);
bOK = bOK && (dwServiceType == INTERNET_SERVICE_HTTP);
if (FALSE == bOK)
{
return -1;
}
//连接服务器
pServer = Session.GetHttpConnection(strServerName,nPort);
pHttpFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_GET, strObject, NULL, 1, NULL, NULL, dwHttpRequestFlags);
pHttpFile->AddRequestHeaders(szHeaders);
pHttpFile->SendRequest();
DWORD dwRet;
pHttpFile->QueryInfoStatusCode(dwRet);
char buff[32];
ULONGLONG nTotalSize = 0;
DWORD dwLength = sizeof(nTotalSize);
if (!pHttpFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER,&nTotalSize, &dwLength))
{
return -1;
}
if (pHttpFile != NULL)
{
pHttpFile->Close();
}
if (pServer != NULL)
{
pServer ->Close();
}
return nTotalSize;
}