|
-
July 16th, 2002, 08:51 AM
#1
HttpQueryInfo problem....
i m unable to get file size of html files on the net.. is the problem with me or is there anykinda protection for not to give file size??
this is my code to get file size ;
int CBase::GetSize(void)
{
//Validate the URL
ASSERT(m_Adres.GetLength()); //Did you forget to specify the file to download
if (!AfxParseURL(m_Adres, m_dwServiceType, m_sServer, m_sObject, m_nPort))
{
//Try sticking "http://" before it
m_Adres = _T("http://") + m_Adres;
if (!AfxParseURL(m_Adres, m_dwServiceType, m_sServer, m_sObject, m_nPort))
{
return -1;
}
}
//Try and open the file we will download into
if (!m_FileToWrite.Open(m_Yer, CFile::modeCreate | CFile::modeWrite ) )
{
return -1;
}
//Create the Internet session handle
// ASSERT(m_hInternetSession == NULL);
m_hInternetSession = ::InternetOpen(AfxGetAppName(), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (m_hInternetSession == NULL)
{
return -1;
}
//Make the connection to the HTTP server
// ASSERT(m_hHttpConnection == NULL);
if (m_sUserName.GetLength())
m_hHttpConnection = ::InternetConnect(m_hInternetSession, m_sServer, m_nPort, m_sUserName,
m_sPassword, m_dwServiceType, 0, (DWORD_PTR)AfxGetApp( ));
else
m_hHttpConnection = ::InternetConnect(m_hInternetSession, m_sServer, m_nPort, NULL,
NULL, m_dwServiceType, 0, (DWORD_PTR)AfxGetApp( ));
if (m_hHttpConnection == NULL)
{
return -1;
}
//Issue the request to read the file
LPCTSTR ppszAcceptTypes[2];
ppszAcceptTypes[0] = _T("*/*"); //We support accepting any mime file type since this is a simple download of a file
ppszAcceptTypes[1] = NULL;
// ASSERT(m_hHttpFile == NULL);
m_hHttpFile = HttpOpenRequest(m_hHttpConnection, NULL, m_sObject, NULL,(LPCSTR)m_Up, ppszAcceptTypes, INTERNET_FLAG_RELOAD |
INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_KEEP_CONNECTION, (DWORD_PTR) AfxGetApp( ));
if (m_hHttpFile == NULL)
{
return -1;
}
//label used to jump to if we need to resend the request
resend:
//Issue the request
BOOL bSend = ::HttpSendRequest(m_hHttpFile, NULL, 0, NULL, 0);
if (!bSend)
{
return -1;
}
//Check the HTTP status code
TCHAR szStatusCode[32];
DWORD dwInfoSize = 32;
if (!HttpQueryInfo(m_hHttpFile, HTTP_QUERY_STATUS_CODE, szStatusCode, &dwInfoSize, NULL))
{
return -1;
}
else
{
long nStatusCode = _ttol(szStatusCode);
//Handle any authentication errors
if (nStatusCode == HTTP_STATUS_PROXY_AUTH_REQ || nStatusCode == HTTP_STATUS_DENIED)
{
// We have to read all outstanding data on the Internet handle
// before we can resubmit request. Just discard the data.
char szData[51];
DWORD dwSize;
do
{
::InternetReadFile(m_hHttpFile, (LPVOID)szData, 50, &dwSize);
}
while (dwSize != 0);
//Bring up the standard authentication dialog
// if (::InternetErrorDlg(GetSafeHwnd(), m_hHttpFile, ERROR_INTERNET_INCORRECT_PASSWORD, FLAGS_ERROR_UI_FILTER_FOR_ERRORS |
// FLAGS_ERROR_UI_FLAGS_GENERATE_DATA | FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS, NULL) == ERROR_INTERNET_FORCE_RETRY)
goto resend;
}
else if (nStatusCode != HTTP_STATUS_OK)
{
return -1;
}
}
// Get the length of the file.
TCHAR szContentLength[32];
dwInfoSize = 32;
DWORD dwFileSize = 0;
BOOL bGotFileSize = FALSE;
if (::HttpQueryInfo(m_hHttpFile, HTTP_QUERY_CONTENT_LENGTH, szContentLength, &dwInfoSize, NULL))
{
//Set the progress control range
bGotFileSize = TRUE;
dwFileSize = (DWORD) _ttol(szContentLength);
// SetProgressRange(dwFileSize);
}
m_size=dwFileSize;
m_FileToWrite.Close();
if (m_bAbort)
: eleteFile(m_Yer);
//We're finished
InternetCloseHandle(m_hHttpFile);
InternetCloseHandle(m_hHttpConnection);
InternetCloseHandle(m_hInternetSession);
return m_size;
}
it does come till the end but ::HttpQueryInfo fails... why???
NylonAge works for a perfect world... and needs help!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|