Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Dowloading Internet Files
Message
From
06/05/2018 16:05:54
 
General information
Forum:
Visual FoxPro
Category:
Windows API functions
Environment versions
Visual FoxPro:
VFP 9 SP2
OS:
Windows 10
Network:
Windows Server 2012
Database:
Firebird
Application:
Desktop
Virtual environment:
VMWare
Miscellaneous
Thread ID:
01659775
Message ID:
01659777
Views:
83
>I need to automate the downloading of files identified in web hyperlinks contained in the body of incoming email messages. No problem using Outlook automation to open the messages and parsing them to extract the links. And calling the Windows API function URLDownlLoadToFile then usually works fine. But problems arise with one company whose messages containing links formatted as http://www.companyname.com/Admin/FormCenter/Submissions/ViewFile/446/?name=PhotoFile.jpg. In those cases URLDownlLoadToFile returns html page code that again contains that link, rather the actual photo file sought. Manually clicking such links from a web browser does present an option to either save or open the needed file, so might the solution involve automating Internet Explorer, or using the WebBrowser control? Or is there an easier way?

Short but not 100% reliable:
*GetFileFfromURL.prg
LPARAMETERS lcRemoteFile,lcLocalFile
DECLARE INTEGER URLDownloadToFile IN urlmon.dll; 
    INTEGER pCaller, STRING szURL, STRING szFileName,; 
    INTEGER dwReserved, INTEGER lpfnCB 

*LOCAL lcRemoteFile, lcLocalFile 
*lcRemoteFile = "http://www.news2news.com/vfp/downloads/w32data.zip" 
*lcLocalFile  = "c:\temp\w32data.zip" 

= URLDownloadToFile (0, lcRemoteFile, lcLocalFile, 0, 0)
Very reliable version:
*GetDataFromURL.prg
LPARAMETERS pcUrlName
DECLARE INTEGER InternetOpen IN wininet.DLL STRING sAgent, ;
      INTEGER lAccessType, STRING sProxyName, ;
      STRING sProxyBypass, INTEGER lFlags

DECLARE INTEGER InternetOpenUrl IN wininet.DLL ;
   INTEGER hInternetSession, STRING sUrl, STRING sHeaders,;
   INTEGER lHeadersLength, INTEGER lFlags, INTEGER lContext

DECLARE INTEGER InternetReadFile IN wininet.DLL INTEGER hfile, ;
      STRING @sBuffer, INTEGER lNumberofBytesToRead, INTEGER @lBytesRead

DECLARE short InternetCloseHandle IN wininet.DLL INTEGER hInst

#DEFINE INTERNET_OPEN_TYPE_PRECONFIG 0
#DEFINE INTERNET_OPEN_TYPE_DIRECT 1
#DEFINE INTERNET_OPEN_TYPE_PROXY 3
#DEFINE SYNCHRONOUS 0
#DEFINE INTERNET_FLAG_RELOAD 2147483648
#DEFINE CR CHR(13)

local lsAgent, lhInternetSession, lhUrlFile, llOk, lnOk, lcRetVal, lcReadBuffer, lnBytesRead

* what application is using Internet services?
lsAgent = "VPF 5.0"

lhInternetSession = InternetOpen( lsAgent, INTERNET_OPEN_TYPE_PRECONFIG, ;
      '', '', SYNCHRONOUS)

* debugging line - uncomment to see session handle
* WAIT WINDOW "Internet session handle: " + LTRIM(STR(hInternetSession))

IF lhInternetSession = 0
   WAIT WINDOW "Internet session cannot be established" TIME 2
   RETURN .null.
ENDIF

lhUrlFile = InternetOpenUrl( lhInternetSession, pcUrlName, '', 0, ;
                             INTERNET_FLAG_RELOAD, 0)

* debugging line - uncomment to see URL handle
* WAIT WINDOW "URL Handle: " + LTRIM(STR(hUrlFile))

IF lhUrlFile = 0
   WAIT WINDOW "URL cannot be opened" Timeout 5
   RETURN .null.
ENDIF

lcRetVal = ""
llOk = .t.

DO WHILE llOK
   * set aside a big buffer
   lsReadBuffer = SPACE(32767)
   lnBytesRead = 0
   lnOK = InternetReadFile( lhUrlFile, @lsReadBuffer, LEN(lsReadBuffer), @lnBytesRead)

   if ( lnBytesRead > 0 )
      lcRetVal = lcRetVal + left( lsReadBuffer, lnBytesRead )
   endif

   * error trap - either a read failure or read past eof()
   llOk = ( lnOK = 1 ) and ( lnBytesRead > 0 )
ENDDO

* close all the handles we opened
InternetCloseHandle( lhUrlFile )
InternetCloseHandle( lhInternetSession )

* return the URL contents
RETURN lcRetVal
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform