Microsoft KB Archive/311306

= How to use Visual FoxPro to download a Web page from the Internet =

Article ID: 311306

Article Last Modified on 2/16/2005

-

APPLIES TO


 * Microsoft Visual FoxPro 6.0 Professional Edition
 * Microsoft Visual FoxPro 7.0 Professional Edition
 * Microsoft Visual FoxPro 8.0 Professional Edition
 * Microsoft Visual FoxPro 9.0 Professional Edition

-



This article was previously published under Q311306



SUMMARY
This article provides sample code that shows how to download the contents of a Web page from the Internet without user interaction or automation of Microsoft Internet Explorer. One sample uses a function from the dynamic-link library Urlmon.dll, and another sample uses the Msinet.ocx ActiveX control.



MORE INFORMATION
Urlmon.dll ships with many Microsoft Products and operating systems:
 * Microsoft Internet Explorer 5.0 and later
 * Microsoft Windows 98 and Microsoft Windows 98 SE
 * Microsoft Windows Millennium Edition (Me)
 * Microsoft Windows 2000 Professional and Microsoft Windows 2000 Server
 * Microsoft Office 2000

Msinet.ocx ships with Microsoft Visual Studio 97 and Microsoft Visual Studio 6.0. Msinet.ocx also ships with the stand-alone versions of the following products:
 * Microsoft Visual Basic 5.0 and Visual Basic 6.0
 * Microsoft Visual FoxPro 6.0 and Visual FoxPro 7.0
 * Microsoft Interdev 6.0
 * Microsoft C++ 5.0 and C++ 6.0

If you are a licensed user of one of these development languages, you are permitted to redistribute the Msinet.ocx control.

How to use the sample code
To use these code samples, follow these steps:
 * 1) Paste each code sample in a new program in Visual FoxPro version 6.0 or later.
 * 2) Save the programs, and then run the programs. As you run each program, a form appears that requests a stock symbol.
 * 3) Type any valid stock symbol in the text box, and then click Get Quote to receive a quote.

Review the comments in the source code for cmdGetQuote.click to understand how the quote was acquired.

Note The Web page that these code samples download may change at any time. As a result, the code that parses the Web page to extract the stock quote may no longer work. However, the concept used to download the page still will apply.

Using Urlmon.dll

 * AUTHOR: Trevor Hancock (TREVORH@MICROSOFT.COM)
 * CREATED: 10/24/01 03:07:28 PM for Microsoft Knowledge Base article Q311306
 * ABSTRACT: Downloads a Web page and then parses the Web page to extract a stock quote.
 * DETAILS: This code uses the function URLDownloadToFile, located in
 * Urlmon.dll. This function downloads a Web page from
 * http://moneycentral.msn.com. Specifically, the function calls
 * &quot;http://moneycentral.msn.com/scripts/webquote.dll?iPage=lqd&Symbol=&quot;,
 * passing to the Web page a stock symbol that the user types into THISFORM.txtSYMBOL.
 * The Web site returns an entire page of information about the
 * stock, which this code then parses to extract the current quote.
 * The Web site returns an entire page of information about the
 * stock, which this code then parses to extract the current quote.

PUBLIC ofrmURLMON

ofrmURLMON = NEWOBJECT(&quot;frmURLMON&quot;) ofrmURLMON.SHOW RETURN

DEFINE CLASS frmURLMON AS FORM HEIGHT = 83 WIDTH = 317 AUTOCENTER = .T.   BORDERSTYLE = 2 CAPTION = &quot;Stock Quote via URLMON.DLL&quot; MAXBUTTON = .F.   MINBUTTON = .F.    NAME = &quot;frmURLMON&quot;

ADD OBJECT txtsymbol AS TEXTBOX WITH ; FONTNAME = &quot;Verdana&quot;, ; VALUE = &quot;MSFT&quot;, ; FORMAT = &quot;!&quot;, ; HEIGHT = 26, ; LEFT = 94, ; TABINDEX = 1, ; TOP = 7, ; WIDTH = 100, ; NAME = &quot;txtSymbol&quot;

ADD OBJECT cmdGetQuote AS COMMANDBUTTON WITH ; TOP = 7, ; LEFT = 206, ; HEIGHT = 27, ; WIDTH = 94, ; FONTNAME = &quot;Verdana&quot;, ; CAPTION = &quot;Get Quote&quot;, ; TABINDEX = 2, ; NAME = &quot;cmdGetQuote&quot;

ADD OBJECT lblSymbol AS LABEL WITH ; AUTOSIZE = .T., ; FONTNAME = &quot;Verdana&quot;, ; CAPTION = &quot;Stock Symbol:&quot;, ; HEIGHT = 16, ; LEFT = 3, ; TOP = 12, ; WIDTH = 90, ; TABINDEX = 5, ; NAME = &quot;lblSymbol&quot;

ADD OBJECT txtQuote AS TEXTBOX WITH ; FONTNAME = &quot;Verdana&quot;, ; HEIGHT = 23, ; LEFT = 94, ; READONLY = .T., ; TOP = 39, ; WIDTH = 100, ; NAME = &quot;txtQuote&quot;

ADD OBJECT lblQuote AS LABEL WITH ; AUTOSIZE = .T., ; FONTNAME = &quot;Verdana&quot;, ; CAPTION = &quot;Quote:&quot;, ; HEIGHT = 16, ; LEFT = 47, ; TOP = 42, ; WIDTH = 46, ; TABINDEX = 5, ; NAME = &quot;lbQuote&quot;

PROCEDURE cmdGetQuote.CLICK LOCAL lcQuote AS STRING, ; lcTempTxtFile AS STRING, ; lnGetResults AS INTEGER, ; lcURL AS STRING

lcQuote = &quot;&quot; *-- Set up a variable referring to a temp .TXT file. *-- Uses the current VFP TEMP DIR [SYS(2023)] *-- and a random file name [SYS(2015)] lcTempTxtFile = FORCEEXT(ADDBS(SYS(2023)) + SYS(2015), &quot;TXT&quot;) lnGetResults = 0 lcGetURl = &quot;http://moneycentral.msn.com/scripts/webquote.dll?iPage=qd&Symbol=&quot; + ; ALLT(THISFORM.txtsymbol.VALUE)

DECLARE LONG URLDownloadToFile IN URLMON.DLL ; LONG, STRING, STRING, LONG, LONG

WITH THISFORM *-- Get the quote using the Urlmon.dll and store it           *-- to a temp .TXT file. *-- This function returns non-zero if it fails. lnGetResults = URLDownloadToFile(0, lcGetURl, lcTempTxtFile, 0, 0) IF lnGetResults # 0 MESSAGEBOX(&quot;Download Failed&quot;,0,&quot;&quot;) RETURN .F.           ENDIF

*-- Read the Web page into a variable, and then erase it. lcQuote = FILETOSTR(lcTempTxtFile) ERASE (lcTempTxtFile)

*-- Grab the quote from the Web page. If in VFP 7.0 or later, we use the new *-- STREXTRACT function to grab the data between two delimiters. *-- In VFP 6.0, use SUBSTR to get the data, and then trim off any *-- trailing HTML with the TRANSFORM AND VAL functions. IF VERSION(5) => 700 lcQuote = STREXTRACT(lcQuote, [Last  ], [&nbsp]) ELSE *-- Extract the quote from the HTML lcQuote = SUBSTR(lcQuote, ATC([Last ], lcQuote) + 16, 8) *-- Trim off the trailing HTML. VAL will only return the numbers, *-- which we then change to a string with TRANSFORM lcQuote = TRANSFORM(VAL(lcQuote)) ENDIF

*-- Put the quote in the text box. .txtQuote.VALUE = &quot;$&quot; + lcQuote ENDWITH ENDPROC ENDDEFINE

Using Msinet.ocx
*---
 * AUTHOR: Trevor Hancock (TREVORH@MICROSOFT.COM)
 * CREATED: 10/24/01 03:07:28 PM for Microsoft Knowledge Base article Q311306
 * ABSTRACT: Downloads a Web page and then parses it to extract a stock quote.
 * DETAILS: This code uses the Msinet.ocx ActiveX control to
 * download a Web page from http://moneycentral.msn.com. Specifically,
 * the function calls &quot;http://moneycentral.msn.com/scripts/webquote.dll?iPage=lqd&Symbol=&quot;,
 * passing to the Web page a stock symbol that the user types into THISFORM.txtSYBMOL.
 * The Web site returns an entire page of information about the
 * stock, which this code then parses to extract the current quote.
 * stock, which this code then parses to extract the current quote.

PUBLIC ofrmMSINET

ofrmMSINET = NEWOBJECT(&quot;frmMSINET&quot;) ofrmMSINET.SHOW RETURN

DEFINE CLASS ocxMSINET AS OLECONTROL OLECLASS = &quot;InetCtls.Inet.1&quot; ENDDEFINE

DEFINE CLASS frmMSINET AS FORM HEIGHT = 70 WIDTH = 304 AUTOCENTER = .T.   BORDERSTYLE = 3 CAPTION = &quot;Stock Quote via MSINET.OCX&quot; MAXBUTTON = .F.   MINBUTTON = .F.    NAME = &quot;frmMSINET&quot;

ADD OBJECT MSINET AS ocxMSINET WITH ; TOP = 37, ; LEFT = 234, ; HEIGHT = 100, ; WIDTH = 100, ; NAME = &quot;MSINET&quot;

ADD OBJECT txtsymbol AS TEXTBOX WITH ; FONTNAME = &quot;Verdana&quot;, ; VALUE = &quot;MSFT&quot;, ; FORMAT = &quot;!&quot;, ; HEIGHT = 26, ; LEFT = 94, ; TABINDEX = 1, ; TOP = 7, ; WIDTH = 100, ; NAME = &quot;txtSymbol&quot;

ADD OBJECT cmdGetQuote AS COMMANDBUTTON WITH ; TOP = 7, ; LEFT = 206, ; HEIGHT = 27, ; WIDTH = 94, ; FONTNAME = &quot;Verdana&quot;, ; CAPTION = &quot;Get Quote&quot;, ; TABINDEX = 2, ; NAME = &quot;cmdGetQuote&quot;

ADD OBJECT lblSymbol AS LABEL WITH ; AUTOSIZE = .T., ; FONTNAME = &quot;Verdana&quot;, ; CAPTION = &quot;Stock Symbol:&quot;, ; HEIGHT = 16, ; LEFT = 3, ; TOP = 12, ; WIDTH = 90, ; TABINDEX = 5, ; NAME = &quot;lblSymbol&quot;

ADD OBJECT txtQuote AS TEXTBOX WITH ; FONTNAME = &quot;Verdana&quot;, ; HEIGHT = 23, ; LEFT = 94, ; READONLY = .T., ; TOP = 39, ; WIDTH = 100, ; NAME = &quot;txtQuote&quot;

ADD OBJECT lblQuote AS LABEL WITH ; AUTOSIZE = .T., ; FONTNAME = &quot;Verdana&quot;, ; CAPTION = &quot;Quote:&quot;, ; HEIGHT = 16, ; LEFT = 47, ; TOP = 42, ; WIDTH = 46, ; TABINDEX = 5, ; NAME = &quot;lbQuote&quot;

PROCEDURE cmdGetQuote.CLICK LOCAL lcQuote AS STRING, ; lcURL AS STRING

lcQuote = &quot;&quot; lcGetURl = &quot;http://moneycentral.msn.com/scripts/webquote.dll?iPage=qd&Symbol=&quot; + ; ALLT(THISFORM.txtsymbol.VALUE)

WITH THISFORM *-- Get the quote using the Msinet.ocx ActiveX control. *-- The control will load it directly into a variable. lcQuote = .MSINET.OpenURL(lcGetURl) IF EMPTY(lcQuote) MESSAGEBOX(&quot;Download Failed.&quot;,0,&quot;&quot;) RETURN .F.           ENDIF

*-- Grab the quote from the Web page. If in VFP 7.0 or later, we use the new *-- STREXTRACT function to grab the data between two delimiters. *-- In VFP 6.0, use SUBSTR to get the data, and then trim off any *-- trailing HTML with the TRANSFORM AND VAL functions. IF VERSION(5) => 700 lcQuote = STREXTRACT(lcQuote, [Last  ], [&nbsp]) ELSE *-- Extract the quote from the HTML lcQuote = SUBSTR(lcQuote, ATC([Last ], lcQuote) + 16, 8) *-- Trim off the trailing HTML. VAL will only return the numbers, *-- which we then change to a string with TRANSFORM lcQuote = TRANSFORM(VAL(lcQuote)) ENDIF *-- Put the quote in the text box. .txtQuote.VALUE = &quot;$&quot; + lcQuote ENDWITH ENDPROC ENDDEFINE

