Solved

Downloading images from a server

Posted on 1998-12-06
5
596 Views
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!
0
Comment
Question by:ennie
  • 3
  • 2
5 Comments
 
LVL 2

Expert Comment

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

0
 

Author Comment

by:ennie
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?
0
 
LVL 2

Expert Comment

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

0
 
LVL 2

Accepted Solution

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

      // Open the new session
      m_hSession = ::InternetOpen("abc",
                              bUseProxy?INTERNET_OPEN_TYPE_PROXY:INTERNET_OPEN_TYPE_PRECONFIG,
                              bUseProxy?(LPCTSTR)strProxyName:(LPCTSTR)NULL,
                              NULL,0);

      // Did it work??
      if(m_hSession != NULL)
      {
            DWORD dwTimeout = 4000;
            ::InternetSetOption(m_hSession,INTERNET_OPTION_CONNECT_TIMEOUT,
                                                      &dwTimeout,sizeof(dwTimeout));
                                    
            ret = SUCCESS;
      }
      else ret = BAD_URL;

      return(ret);
}
DoGet(CString& strURL, BYTE **pbyBuffer, int& nLen,
                                                                  BOOL bIsTextPage)
{
      RESULTS nRetCode = UNKNOWN_ERROR;
      CString strTempURL = strURL;
      HINTERNET hConnect = NULL;
      HINTERNET hURL = NULL;
   
            INTERNET_PORT nPort;
            DWORD dwServiceType;

            if(!AfxParseURLEx(strTempURL,dwServiceType,strServer,strObject,nPort,
                                    strUser,strPassword,ICU_NO_ENCODE))
                  return(BAD_URL);

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

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

            // Send the "get" request
             ::HttpSendRequest(hURL,NULL,0,0,0);

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

            // Check the return code
            DWORD dwRet, dwLen;
            char szTemp[500];
            dwLen = sizeof(szTemp);
            ::HttpQueryInfo(hURL,HTTP_QUERY_STATUS_CODE,szTemp,&dwLen,NULL);
            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;
                  ::HttpQueryInfo(hURL,HTTP_QUERY_LOCATION,NULL,&dwLen,0);
                  ::HttpQueryInfo(hURL,HTTP_QUERY_LOCATION,
                                    strNewLocation.GetBufferSetLength(dwLen),
                                    &dwLen,0);
                  strNewLocation.ReleaseBuffer(dwLen);

                  // Make sure that the new address contains a server name too
                  CString strNewServer;
                  AfxParseURLEx(strNewLocation,dwServiceType,strNewServer,strObject,nPort,
                                          strUser,strPassword,ICU_NO_ENCODE);

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

                  // Save the new location
                  strURL = strNewLocation;

                  ::InternetCloseHandle(hURL);
                  ::InternetCloseHandle(hConnect);
                  return(LOCATION_MOVED);
            }
            else if(dwRet == HTTP_STATUS_REQUEST_TIMEOUT ||
                              dwRet == HTTP_STATUS_GATEWAY_TIMEOUT ||
                              dwRet == HTTP_STATUS_NOT_FOUND)
            {
                  Sleep(500L);
                  ::InternetCloseHandle(hURL);
                  ::InternetCloseHandle(hConnect);
                  return(TIMEOUT);
            }
            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);
                  ::HttpQueryInfo(hURL,HTTP_QUERY_CONTENT_LENGTH,szTemp,&dwLen,NULL);
                  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
                  while(bOkay)
                  {
                        // 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
                  ::InternetCloseHandle(hURL);
                  ::InternetCloseHandle(hConnect);

                  return(SUCCESS);
            }
}



0
 

Author Comment

by:ennie
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.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Named range not carried over 10 65
Modbus - whats the maximum I can store in one register? 4 80
Making an alias 7 101
Message not shown 5 67
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

809 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