CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: HttpQueryInfo problem....

  1. #1
    Join Date
    Jan 2002
    Location
    CenterOfTheWorld
    Posts
    203

    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!

  2. #2
    Join Date
    Jan 2001
    Location
    Toronto, Ontario, Canada
    Posts
    62

    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.

  3. #3
    Join Date
    Jan 2002
    Location
    CenterOfTheWorld
    Posts
    203

    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!

  4. #4
    Join Date
    Jan 2001
    Location
    Toronto, Ontario, Canada
    Posts
    62

    Smile 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.

  5. #5
    Join Date
    Oct 2007
    Posts
    2

    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..

  6. #6
    Join Date
    Aug 2001
    Location
    Germany
    Posts
    1,384

    Re: HttpQueryInfo problem not able to get 200 status.

    What does HttpQueryInfo returns? I don't see that you are checking bRet.

    Regards,
    Usman.

  7. #7
    Join Date
    Oct 2007
    Posts
    2

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)