|
-
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!
-
July 16th, 2002, 09:17 AM
#2
Why not?
DWORD dwContentLength;
DWORD dwCodesize;
dwCodesize=sizeof(dwContentLength);
if(!HttpQueryInfo (hRequest, //handle of HttpOpenRequest ()
HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, //Receives the status code returned by the server.
&dwContentLength, //ÎÞÀ*ÐÍÖ¸Õ룬ָÏò·µ»Ø×´Ì¬ÐÅÏ¢
&dwCodeSize, //»ñÈ¡Êý¾Ý»º³åÇø³¤¶È
NULL)){
...
}
// Now use dwContentLength as what you would.
-
July 16th, 2002, 10:09 AM
#3
no... it did not work...
let me remind u something..
i got the size of jpg files with that code but not the html files..???
dwContentLength=200 for the code u give me.. it gives me always the same code..
Do u have any idea?
NylonAge works for a perfect world... and needs help!
-
July 16th, 2002, 05:15 PM
#4
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.
-
October 15th, 2007, 06:40 AM
#5
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..
-
October 15th, 2007, 07:20 AM
#6
Re: HttpQueryInfo problem not able to get 200 status.
What does HttpQueryInfo returns? I don't see that you are checking bRet.
Regards,
Usman.
-
October 16th, 2007, 12:00 AM
#7
Re: HttpQueryInfo problem....
httpqueryinfo() retruns TRUE.
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
|