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)
::DeleteFile(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???
Sorry, I posted the wrong code here.
Sorry, I posted the wrong code here.
I actually used:
if(!InternetQueryDataAvailable(hRequest,
&dwInfoSize, //Optional. Long pointer to a variable
0, //that receives the number of available bytes.
0)){
// Error clean up
return;
}
// Allocate memory and do what you need to do.
Hope this helps.:)
HttpQueryInfo problem not able to get 200 status.
hi all
i have some problem with this code. i want to get the status code as 200 but i am getting the status code as 0 or sometimes 2 , can you please tell me how can i get the status code as 200.
here is the code
unsigned char szTemp[4];
unsigned int dwRet ;
dwRet = 4;
bRet = HttpQueryInfo (pContext->ReadSession, HTTP_QUERY_STATUS_CODE,
szTemp, (LPDWORD)&dwRet, NULL);
long int l = atol((const char*)szTemp) ;
printf (l) ;
here l = 0 or 2 comes.. but i want 'l' to be 200 i.e "Success"
please help me..
Re: HttpQueryInfo problem not able to get 200 status.
What does HttpQueryInfo returns? I don't see that you are checking bRet.
Regards,
Usman.
Re: HttpQueryInfo problem....
httpqueryinfo() retruns TRUE.