Downloading images from a server

Posted on 1998-12-06
Medium Priority
Last Modified: 2013-11-20
I'm pretty new at this, and I need all the help I can get. I have a browser application, and it needs to be able to download images from a server (so it won't need to have the file locally for it to be able to show it).  The way the browser works is pretty much like the regular browsers, except that instead of HTML, we use our own scripting conventions. How do I go about this? I'm programming using C++/MFC.  Thanks!
Question by:ennie
  • 3
  • 2

Expert Comment

ID: 1325802
WInternetAPI .
CInternetSession can create the CHttpFile object.
CHttpFile can downLoad file from http server.


Author Comment

ID: 1325803
Thanks... At least I know where to start.
I forgot to mention that after downloading the image, some sort of caching system has to be implemented.  Are there MFC classes that also handle this?

Expert Comment

ID: 1325804
no i think the cache is associate the application.
MFC will not handle this, you need to implement the cache yourself .


Accepted Solution

wyy_cq earned 100 total points
ID: 1325805
there is some demo code:
YourOpenSession(BOOL bUseProxy, CString& strProxyName)

      // Open the new session
      m_hSession = ::InternetOpen("abc",

      // Did it work??
      if(m_hSession != NULL)
            DWORD dwTimeout = 4000;
            ret = SUCCESS;
      else ret = BAD_URL;

DoGet(CString& strURL, BYTE **pbyBuffer, int& nLen,
                                                                  BOOL bIsTextPage)
      CString strTempURL = strURL;
      HINTERNET hConnect = NULL;
            INTERNET_PORT nPort;
            DWORD dwServiceType;


            // Establish a server connection
            hConnect = ::InternetConnect(m_hSession,strServer,
                                          0) ;

            hURL = ::HttpOpenRequest(hConnect,"GET",strObject,
                                    szTempX,dwHttpRequestFlags,0) ;

            // Send the "get" request

            // If it failed, then get out now
          if(hURL == NULL)

            // Check the return code
            DWORD dwRet, dwLen;
            char szTemp[500];
            dwLen = sizeof(szTemp);
            dwRet = (DWORD) atoi(szTemp);
            dwLen = sizeof(szTemp);
            m_dwRet = dwRet;

            // Was this page redirected?
            if(dwRet == HTTP_STATUS_MOVED || dwRet == HTTP_STATUS_REDIRECT ||
                         dwRet == HTTP_STATUS_REDIRECT_METHOD)
                  // Yep...get the new base location
                  CString strNewLocation;

                  // Make sure that the new address contains a server name too
                  CString strNewServer;

                  // Is the server name missing for some reason??
                        // Yes, add it back in
                        CString strTempLocation = strServer;
                        if(strNewLocation.Left(1) != "/")
                              strTempLocation += "/";
                        strTempLocation += strNewLocation;
                        strNewLocation = strTempLocation;

                  // Save the new location
                  strURL = strNewLocation;

            else if(dwRet == HTTP_STATUS_REQUEST_TIMEOUT ||
                              dwRet == HTTP_STATUS_GATEWAY_TIMEOUT ||
                              dwRet == HTTP_STATUS_NOT_FOUND)
            else if(dwRet == HTTP_STATUS_OK)
                  // Get the data for the file
                  BYTE *pbyTempBuffer = *pbyBuffer;
                  DWORD dwTotalBytes = 0;
                  DWORD dwBytesRead;
                  BOOL bOkay = TRUE;

                  // Find out how much data we have
                  dwLen = sizeof(szTemp);
                  dwLen = (DWORD) atol(szTemp);

                  // Just make sure we didn't get a stupid length value back
                  if(dwLen <= 0)
                        dwLen = 10000;

                  // Keep reading data until we've gotten it all
                        // Read the data
                        bOkay = ::InternetReadFile(hURL,pbyTempBuffer,dwLen,&dwBytesRead);

                        // Add to the counts
                        if(bOkay && dwBytesRead > 0 && dwTotalBytes < MAX_INET_BUFFER-1)
                              dwTotalBytes += dwBytesRead;
                              pbyTempBuffer += dwBytesRead;
                        else break;

                  // NULL terminate the buffer
                  pbyTempBuffer[0] = '\0';
                  nLen = dwTotalBytes;

                  // Close the file



Author Comment

ID: 1325806
I noticed that you did not use the CInternetSession class as you have previously suggested.  Would that be another solution aside from the code you just sent?
In any case, would you know which class CInternetSession uses (CAsync / QSocket...). Our application uses QSocket for networking, and I was wondering if I could somehow mix CInternet with QSocket. If not, will your proposed answer still work with QSocket?  Thanks.

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
If you are looking for an automated tool which can generate reports for Outlook emails and other items from PST file, then you can go for Kernel PST Reporter tool. The reports which are created by this tool are helpful to analyze and understand PST …

600 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question