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. Download file from the url.

Download file from the url.

Scheduled Pinned Locked Moved C / C++ / MFC
helpcomperformancetutorial
9 Posts 4 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.
  • A Offline
    A Offline
    avika
    wrote on last edited by
    #1

    We want to download a file from the url. If we are download a text file then this code will download the file successfully, but if we are trying to download a exe or even a jpg file then this code does not work fine. We are using following code - DWORD dwSize = 0; DWORD dwDownloaded = 0; LPSTR pszOutBuffer; BOOL bResults = FALSE; HINTERNET hSession = NULL, hConnect = NULL, hRequest = NULL; hSession = WinHttpOpen( L"WinHTTP Example/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0); if (hSession) hConnect = WinHttpConnect(hSession, L"localhost.com", INTERNET_DEFAULT_HTTP_PORT, 0); if (hConnect) hRequest = WinHttpOpenRequest( hConnect, L"GET", L"/CMS/upload/1.txt", NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_REFRESH); if (hRequest) bResults = WinHttpSendRequest( hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0); if (bResults) bResults = WinHttpReceiveResponse( hRequest, NULL); if (bResults) { dwSize = 0; if (!WinHttpQueryDataAvailable( hRequest, &dwSize)) printf("Error %u in WinHttpQueryDataAvailable.\n", GetLastError()); if (dwSize > 0) { pszOutBuffer = new char[dwSize+1]; } if (!pszOutBuffer) { printf("Out of memory\n"); dwSize=0; } else { if (dwSize>0) { ZeroMemory(pszOutBuffer, dwSize+1); if (!WinHttpReadData( hRequest, (LPVOID)pszOutBuffer, dwSize, &dwDownloaded)) printf("Error %u in WinHttpReadData.\n", GetLastError()); } } HANDLE hFile; DWORD dwNumWritten; BOOL bTest; ofstream outfile ("D:\\1.txt",ofstream::binary); outfile.write(pszOutBuffer,sizeof(dwSize) * 50); Please help me what I am doing wrong.

    _ CPalliniC W 3 Replies Last reply
    0
    • A avika

      We want to download a file from the url. If we are download a text file then this code will download the file successfully, but if we are trying to download a exe or even a jpg file then this code does not work fine. We are using following code - DWORD dwSize = 0; DWORD dwDownloaded = 0; LPSTR pszOutBuffer; BOOL bResults = FALSE; HINTERNET hSession = NULL, hConnect = NULL, hRequest = NULL; hSession = WinHttpOpen( L"WinHTTP Example/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0); if (hSession) hConnect = WinHttpConnect(hSession, L"localhost.com", INTERNET_DEFAULT_HTTP_PORT, 0); if (hConnect) hRequest = WinHttpOpenRequest( hConnect, L"GET", L"/CMS/upload/1.txt", NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_REFRESH); if (hRequest) bResults = WinHttpSendRequest( hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0); if (bResults) bResults = WinHttpReceiveResponse( hRequest, NULL); if (bResults) { dwSize = 0; if (!WinHttpQueryDataAvailable( hRequest, &dwSize)) printf("Error %u in WinHttpQueryDataAvailable.\n", GetLastError()); if (dwSize > 0) { pszOutBuffer = new char[dwSize+1]; } if (!pszOutBuffer) { printf("Out of memory\n"); dwSize=0; } else { if (dwSize>0) { ZeroMemory(pszOutBuffer, dwSize+1); if (!WinHttpReadData( hRequest, (LPVOID)pszOutBuffer, dwSize, &dwDownloaded)) printf("Error %u in WinHttpReadData.\n", GetLastError()); } } HANDLE hFile; DWORD dwNumWritten; BOOL bTest; ofstream outfile ("D:\\1.txt",ofstream::binary); outfile.write(pszOutBuffer,sizeof(dwSize) * 50); Please help me what I am doing wrong.

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      You could use the URLDownloadToFile API.

      «_Superman_» I love work. It gives me something to do between weekends.
      Microsoft MVP (Visual C++)

      A 1 Reply Last reply
      0
      • A avika

        We want to download a file from the url. If we are download a text file then this code will download the file successfully, but if we are trying to download a exe or even a jpg file then this code does not work fine. We are using following code - DWORD dwSize = 0; DWORD dwDownloaded = 0; LPSTR pszOutBuffer; BOOL bResults = FALSE; HINTERNET hSession = NULL, hConnect = NULL, hRequest = NULL; hSession = WinHttpOpen( L"WinHTTP Example/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0); if (hSession) hConnect = WinHttpConnect(hSession, L"localhost.com", INTERNET_DEFAULT_HTTP_PORT, 0); if (hConnect) hRequest = WinHttpOpenRequest( hConnect, L"GET", L"/CMS/upload/1.txt", NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_REFRESH); if (hRequest) bResults = WinHttpSendRequest( hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0); if (bResults) bResults = WinHttpReceiveResponse( hRequest, NULL); if (bResults) { dwSize = 0; if (!WinHttpQueryDataAvailable( hRequest, &dwSize)) printf("Error %u in WinHttpQueryDataAvailable.\n", GetLastError()); if (dwSize > 0) { pszOutBuffer = new char[dwSize+1]; } if (!pszOutBuffer) { printf("Out of memory\n"); dwSize=0; } else { if (dwSize>0) { ZeroMemory(pszOutBuffer, dwSize+1); if (!WinHttpReadData( hRequest, (LPVOID)pszOutBuffer, dwSize, &dwDownloaded)) printf("Error %u in WinHttpReadData.\n", GetLastError()); } } HANDLE hFile; DWORD dwNumWritten; BOOL bTest; ofstream outfile ("D:\\1.txt",ofstream::binary); outfile.write(pszOutBuffer,sizeof(dwSize) * 50); Please help me what I am doing wrong.

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        avika wrote:

        but if we are trying to download a exe or even a jpg file then this code does not work fine.

        What do you mean exactly? BTW you should use the 'code block' button to post code.

        avika wrote:

        outfile.write(pszOutBuffer,sizeof(dwSize) * 50);

        BTW2 Why 50 in the above line?

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        In testa che avete, signor di Ceprano?

        A 1 Reply Last reply
        0
        • CPalliniC CPallini

          avika wrote:

          but if we are trying to download a exe or even a jpg file then this code does not work fine.

          What do you mean exactly? BTW you should use the 'code block' button to post code.

          avika wrote:

          outfile.write(pszOutBuffer,sizeof(dwSize) * 50);

          BTW2 Why 50 in the above line?

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          A Offline
          A Offline
          avika
          wrote on last edited by
          #4

          if We are pass only outfile.write(pszOutBuffer,sizeof(dwSize)); then it show less data in the text file. But if We pass the outfile.write(pszOutBuffer,sizeof(dwSize) * 50); then it show complete data thats why we are passing this.

          CPalliniC 1 Reply Last reply
          0
          • _ _Superman_

            You could use the URLDownloadToFile API.

            «_Superman_» I love work. It gives me something to do between weekends.
            Microsoft MVP (Visual C++)

            A Offline
            A Offline
            avika
            wrote on last edited by
            #5

            I have tried the method which are suggest by u, Superman but it gives following error --- unresolved external symbol _URLDownloadToFileW@20 referenced in function "void __cdecl funDownloadFile(void)" (?funDownloadFile@@YAXXZ)

            _ 1 Reply Last reply
            0
            • A avika

              if We are pass only outfile.write(pszOutBuffer,sizeof(dwSize)); then it show less data in the text file. But if We pass the outfile.write(pszOutBuffer,sizeof(dwSize) * 50); then it show complete data thats why we are passing this.

              CPalliniC Offline
              CPalliniC Offline
              CPallini
              wrote on last edited by
              #6

              avika wrote:

              outfile.write(pszOutBuffer,sizeof(dwSize) * 50);

              That should be

              outfile.write(pszOutBuffer,dwSize);

              :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              In testa che avete, signor di Ceprano?

              1 Reply Last reply
              0
              • A avika

                I have tried the method which are suggest by u, Superman but it gives following error --- unresolved external symbol _URLDownloadToFileW@20 referenced in function "void __cdecl funDownloadFile(void)" (?funDownloadFile@@YAXXZ)

                _ Offline
                _ Offline
                _Superman_
                wrote on last edited by
                #7

                Add Urlmon.lib to Project Properties -> Configuration Properties -> Linker -> Input -> Additional Dependencies.

                «_Superman_» I love work. It gives me something to do between weekends.
                Microsoft MVP (Visual C++)

                A 1 Reply Last reply
                0
                • _ _Superman_

                  Add Urlmon.lib to Project Properties -> Configuration Properties -> Linker -> Input -> Additional Dependencies.

                  «_Superman_» I love work. It gives me something to do between weekends.
                  Microsoft MVP (Visual C++)

                  A Offline
                  A Offline
                  avika
                  wrote on last edited by
                  #8

                  Now it is working fine ... thanks a lot buddy..

                  1 Reply Last reply
                  0
                  • A avika

                    We want to download a file from the url. If we are download a text file then this code will download the file successfully, but if we are trying to download a exe or even a jpg file then this code does not work fine. We are using following code - DWORD dwSize = 0; DWORD dwDownloaded = 0; LPSTR pszOutBuffer; BOOL bResults = FALSE; HINTERNET hSession = NULL, hConnect = NULL, hRequest = NULL; hSession = WinHttpOpen( L"WinHTTP Example/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0); if (hSession) hConnect = WinHttpConnect(hSession, L"localhost.com", INTERNET_DEFAULT_HTTP_PORT, 0); if (hConnect) hRequest = WinHttpOpenRequest( hConnect, L"GET", L"/CMS/upload/1.txt", NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_REFRESH); if (hRequest) bResults = WinHttpSendRequest( hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0); if (bResults) bResults = WinHttpReceiveResponse( hRequest, NULL); if (bResults) { dwSize = 0; if (!WinHttpQueryDataAvailable( hRequest, &dwSize)) printf("Error %u in WinHttpQueryDataAvailable.\n", GetLastError()); if (dwSize > 0) { pszOutBuffer = new char[dwSize+1]; } if (!pszOutBuffer) { printf("Out of memory\n"); dwSize=0; } else { if (dwSize>0) { ZeroMemory(pszOutBuffer, dwSize+1); if (!WinHttpReadData( hRequest, (LPVOID)pszOutBuffer, dwSize, &dwDownloaded)) printf("Error %u in WinHttpReadData.\n", GetLastError()); } } HANDLE hFile; DWORD dwNumWritten; BOOL bTest; ofstream outfile ("D:\\1.txt",ofstream::binary); outfile.write(pszOutBuffer,sizeof(dwSize) * 50); Please help me what I am doing wrong.

                    W Offline
                    W Offline
                    Whuili_2004
                    wrote on last edited by
                    #9

                    This above code works for binary image download, but the problem is: For an image file with 18k size, only 4k can be download and just half of the image can be seen on the client side. Any ideas? Thanks.

                    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