Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to determine the size of a file retrieved by http protocol

How to determine the size of a file retrieved by http protocol

Scheduled Pinned Locked Moved C / C++ / MFC
tutorial
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gecool
    wrote on last edited by
    #1

    Hy everyone. My purpose is to download a range of bytes (i.e. from the beggining up to a certain point) from a file located by a given URL. I am trying to determine the remote file size, but so far I didn't succeded. I am using WinInet, InternetReadFile function to actually read the file. I can read it, but I would like to determine it's size prior of downloading the whole file. If anyone could show me another way tp do that, more simple than this please do so. THANKS.:)

    H 1 Reply Last reply
    0
    • G gecool

      Hy everyone. My purpose is to download a range of bytes (i.e. from the beggining up to a certain point) from a file located by a given URL. I am trying to determine the remote file size, but so far I didn't succeded. I am using WinInet, InternetReadFile function to actually read the file. I can read it, but I would like to determine it's size prior of downloading the whole file. If anyone could show me another way tp do that, more simple than this please do so. THANKS.:)

      H Offline
      H Offline
      Hans Ruck
      wrote on last edited by
      #2

      Try:

      HttpQueryInfo(hResource, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER,
      &dwContentLength, &dwSize, NULL)

      where hResource is the one you use when calling InternetReadFile, dwContentLength is a DWORD to return the file-size into and dwSize = sizeof(DWORD).


      "though nothing  will keep us together  we can beat them  for ever and ever" rechi

      G 1 Reply Last reply
      0
      • H Hans Ruck

        Try:

        HttpQueryInfo(hResource, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER,
        &dwContentLength, &dwSize, NULL)

        where hResource is the one you use when calling InternetReadFile, dwContentLength is a DWORD to return the file-size into and dwSize = sizeof(DWORD).


        "though nothing  will keep us together  we can beat them  for ever and ever" rechi

        G Offline
        G Offline
        gecool
        wrote on last edited by
        #3

        Thanx Rechi ;-) One extra question ? R U from Romania ? I tried that, but unfortunately i get: ERROR_HTTP_HEADER_NOT_FOUND ... At first i had this in mind that is to play with such functions like Internetqueryoption and stuff, but no matter how much I dig up those flags I didn't find anyone related to the whole file size, just related to sections ... THANKZ anyway :->

        H 1 Reply Last reply
        0
        • G gecool

          Thanx Rechi ;-) One extra question ? R U from Romania ? I tried that, but unfortunately i get: ERROR_HTTP_HEADER_NOT_FOUND ... At first i had this in mind that is to play with such functions like Internetqueryoption and stuff, but no matter how much I dig up those flags I didn't find anyone related to the whole file size, just related to sections ... THANKZ anyway :->

          H Offline
          H Offline
          Hans Ruck
          wrote on last edited by
          #4

          gecool wrote: ERROR_HTTP_HEADER_NOT_FOUND It's about the headers, take a look at this (old) fragment:

            HINTERNET hResource=::HttpOpenRequest(hConnection, \_T("GET"),
              szRelativeFile, NULL, NULL, NULL,
              INTERNET\_FLAG\_KEEP\_CONNECTION, 0);
            
            if (hResource)
            {
              LPTSTR szHeaders=\_T("Accept: audio/x-aiff, application/octet-stream, "
                "application/x-msdownload, audio/basic, "
                "audio/midi, audio/mpeg, audio/wav, image/jpeg, image/gif, "
                "image/jpg, image/png, image/mng, image/bmp, text/plain, "
                "text/html, text/htm\\r\\n");
              CString szAgentHeader;
              szAgentHeader.Format(\_T("User-Agent: %s/1.0\\r\\n"),
                (LPCTSTR)m\_szAppName);
              HttpAddRequestHeaders(hResource, szHeaders, \_tcslen(szHeaders),
                HTTP\_ADDREQ\_FLAG\_ADD\_IF\_NEW);
              HttpAddRequestHeaders(hResource, (LPCTSTR)szAgentHeader,
                szAgentHeader.GetLength(), HTTP\_ADDREQ\_FLAG\_ADD\_IF\_NEW);
              if (HttpSendRequest(hResource, NULL, 0, NULL, 0))
              {
                DWORD dw=0, dwOut=0, i=0, n=0, dwContentLength,
                  dwSize=sizeof(DWORD);
                CString szFileFromPath=CUpgrader::GetFileFromPath(
                  szRelativeFile), filepath;
          
                if (!::HttpQueryInfo(hResource, HTTP\_QUERY\_CONTENT\_LENGTH |
                  HTTP\_QUERY\_FLAG\_NUMBER, &dwContentLength, &dwSize, NULL))
                    dwContentLength=-1; // unknown
          

          and check if something is missing in your code. It's from an update system i wrote a year ago, and it works like a dream :-> gecool wrote: R U from Romania ? Yeah! :-D


          "though nothing  will keep us together  we can beat them  for ever and ever" rechi

          G 1 Reply Last reply
          0
          • H Hans Ruck

            gecool wrote: ERROR_HTTP_HEADER_NOT_FOUND It's about the headers, take a look at this (old) fragment:

              HINTERNET hResource=::HttpOpenRequest(hConnection, \_T("GET"),
                szRelativeFile, NULL, NULL, NULL,
                INTERNET\_FLAG\_KEEP\_CONNECTION, 0);
              
              if (hResource)
              {
                LPTSTR szHeaders=\_T("Accept: audio/x-aiff, application/octet-stream, "
                  "application/x-msdownload, audio/basic, "
                  "audio/midi, audio/mpeg, audio/wav, image/jpeg, image/gif, "
                  "image/jpg, image/png, image/mng, image/bmp, text/plain, "
                  "text/html, text/htm\\r\\n");
                CString szAgentHeader;
                szAgentHeader.Format(\_T("User-Agent: %s/1.0\\r\\n"),
                  (LPCTSTR)m\_szAppName);
                HttpAddRequestHeaders(hResource, szHeaders, \_tcslen(szHeaders),
                  HTTP\_ADDREQ\_FLAG\_ADD\_IF\_NEW);
                HttpAddRequestHeaders(hResource, (LPCTSTR)szAgentHeader,
                  szAgentHeader.GetLength(), HTTP\_ADDREQ\_FLAG\_ADD\_IF\_NEW);
                if (HttpSendRequest(hResource, NULL, 0, NULL, 0))
                {
                  DWORD dw=0, dwOut=0, i=0, n=0, dwContentLength,
                    dwSize=sizeof(DWORD);
                  CString szFileFromPath=CUpgrader::GetFileFromPath(
                    szRelativeFile), filepath;
            
                  if (!::HttpQueryInfo(hResource, HTTP\_QUERY\_CONTENT\_LENGTH |
                    HTTP\_QUERY\_FLAG\_NUMBER, &dwContentLength, &dwSize, NULL))
                      dwContentLength=-1; // unknown
            

            and check if something is missing in your code. It's from an update system i wrote a year ago, and it works like a dream :-> gecool wrote: R U from Romania ? Yeah! :-D


            "though nothing  will keep us together  we can beat them  for ever and ever" rechi

            G Offline
            G Offline
            gecool
            wrote on last edited by
            #5

            Mersi mult ;-) ;-) ;-) Tocmai asta asteptam sa gasesc un piece of code .... ;-) ;-);-) Geo.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups