Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

INET - Why Doesn't this work? 1

Status
Not open for further replies.

craigsboyd

IS-IT--Management
Nov 9, 2002
2,839
US
According to this...


...the following code should work, but doesn't from where i am sitting...cut and paste the following into prg and run to see what i'm talking about...

PUBLIC oForm

oForm = CREATEOBJECT("ftpopenurl")
oForm.show()

DEFINE CLASS ftpopenurl AS form

DoCreate = .T.
Caption = "FTP INET TEST"
Name = "ftpopenurl"

ADD OBJECT inet1 as Inet WITH ;
Top = 12, ;
Left = 12, ;
Height = 100, ;
Width = 100, ;
Name = "Inet1"


ADD OBJECT edit1 AS editbox WITH ;
Height = 120, ;
Left = 12, ;
Top = 72, ;
Width = 348, ;
Name = "Edit1"


ADD OBJECT command1 AS commandbutton WITH ;
Top = 204, ;
Left = 276, ;
Height = 27, ;
Width = 84, ;
Caption = "Command1", ;
Name = "Command1"

PROCEDURE command1.Click
thisform.Edit1.value = thisform.Inet1.OpenURL("ftp://ftp.microsoft.com/MISC/ReadMe1.txt",0)
thisform.Edit1.refresh()
ENDPROC


ENDDEFINE

Define Class INET As OleControl
OleClass='InetCtls.Inet.1'
Enddefine


Slighthaze = NULL
 
Slighthaze = NULL

It maybe be just the URL that is no good, because the following works.

Code:
PROCEDURE command1.Click
        thisform.Edit1.value = thisform.Inet1.OpenURL("[URL unfurl="true"]www.slpcanada.com",0)[/URL]
        thisform.Edit1.refresh()
    ENDPROC


Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Yeah, but that's using http isn't it? I can get it to use http just like it says it should, but FTP is another matter entirely...if you go to ftp://ftp.microsoft.com/Misc/ in your browser or ftp client you can see that the txt file exists and anonymous login is allowed... it just won't pull back the contents like the msdn example says it will...I'm just using that txt file as an example, I would love to see any example with any txt file from any ftp site that will work with the openurl method of the Inet control.

Slighthaze = NULL
 
Code:
LPARAMETERS pcUrlName, oUrlTherm
LOCAL cCmd, nBitSwitch, ncount

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, lnBytesTotal
* what application is using Internet services?
lsAgent = gcprogname
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
   RETURN ''
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
   RETURN ''
ENDIF

lcRetVal = ""
llOk = .t.
lnBytesTotal = 0
nBitSwitch = 0
ncount = 0 
DO WHILE llOK
	** try to trap for ftp connection stuck
	ncount = ncount + 1 
	if ncount = 1000
		return ''
	endif
   * 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

   IF VARTYPE(oUrlTherm) == 'O'
   	   lnBytesTotal = lnBytesTotal + lnBytesRead
   	   oUrlTherm.Update(lnBytesTotal)
	   DOEVENTS
   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

Example of a call to this proc

Code:
IF UPPER(LEFT(m.cServdir,4))=='FTP:' OR UPPER(LEFT(m.cServdir,5))=='HTTP:'
	lRemoteSource = .T.
	nfoundserver = -1	&& Assume that we will not be able to find the server.
	thisform.setmessage('Checking for Updates') 
	cVerInfo = READURL(m.cServdir + 'version.ver')	&& Get version information from FTP site.
*	wait window 'Verinfo '+cverinfo
	IF !EMPTY((cVerInfo))
		cFile = RIGHT(SYS(2015),8) + '.DBF'
		STRTOFILE(cVerInfo, cFile)
		USE (cFile) IN 0 ALIAS SvrVerInfo EXCL
		SELECT SvrVerInfo
		INDEX ON Property TAG Property
		SET ORDER TO Property
		IF SEEK('VERSION')
			aSvVerInfo[4] = ALLTRIM(SvrVerInfo.VALUE)
			nfoundserver = 0	&& Assume that we can find the server now
		ENDIF
	ENDIF
	thisform.setmessage('') 
endif

Please note I pulled this from an existing project and did not spend a lot of time editing out extra stuff, but it should give you something to work with.


 
fluteplr,

Thank you for the example using the API calls. I really need this to work with the Internet Transfer Control (INET), and if it isn't possible with the INET then I need to know why since a vast portion of code will need to be rewritten to use the API calls instead. Had I been writing this thing from scratch I would have used the API calls and kept from having to register INET...but as it is I am working with existing code in an existing project. I am pasting your example into my library of code snippets though and do appreciate your time.

Slighthaze = NULL
 
well here's what I end up doing works good, but seems to be a bit of a hack (this example is jsut a start, I have to still get rid of the do loop and go with getchunk from in the state change area, but wanted to show a down-and-dirty way to get this done) I am still very interested why openurl won't work...

PUBLIC oForm

oForm = CREATEOBJECT("ftpopenurl")
oForm.show()

DEFINE CLASS ftpopenurl AS form

DoCreate = .T.
Caption = "FTP INET TEST"
Name = "ftpopenurl"

ADD OBJECT inet1 as Inet WITH ;
Top = 12, ;
Left = 12, ;
Height = 100, ;
Width = 100, ;
Name = "Inet1"


ADD OBJECT edit1 AS editbox WITH ;
Height = 120, ;
Left = 12, ;
Top = 72, ;
Width = 348, ;
Name = "Edit1"


ADD OBJECT command1 AS commandbutton WITH ;
Top = 204, ;
Left = 276, ;
Height = 27, ;
Width = 84, ;
Caption = "Command1", ;
Name = "Command1"

PROCEDURE command1.Click
LOCAL sTempFile
sTempFile = addbs(SYS(2023))+"_"+SUBSTR(SYS(2015), 4)+".txt"
thisform.Inet1.Execute("ftp://ftp.microsoft.com", "GET MISC/ReadMe1.txt " + sTempFile)
*!* thisform.Edit1.value = thisform.Inet1.OpenURL("ftp://ftp.microsoft.com/MISC/ReadMe1.txt",0)
DO WHILE thisform.Inet1.stillexecuting
DOEVENTS
ENDDO
IF FILE(sTempFile)
thisform.Edit1.value = FILETOSTR(sTempFile)
thisform.Refresh()
ERASE (sTempFile)
endif
ENDPROC


ENDDEFINE

Define Class INET As OleControl
OleClass='InetCtls.Inet.1'
Enddefine


Slighthaze = NULL
 
As far as I know,

thisform.Inet1.Execute("ftp://ftp.microsoft.com";, "GET MISC/ReadMe1.txt " .....

is the way INET has to be used for GET and PUT.

If you decide to use plain old API's, be sure and check out my FAQ:

How do I transfer files using FTP?
faq184-3234


Dave S.
[cheers]
 
Dave,

Got that FAQ in my library and went through your website (some good links in there too). :)

If you look at the page...


...part way down there is a line of code that they say will pull back the contents of a file on an ftp server...

Text1.Text = Inet1. _
OpenURL("ftp://ftp.microsoft.com/disclaimer.txt")

...now the text file disclaimer.txt doesn't exist there, but even when you point it to a text file that does exist on an ftp server it doesn't work! The only reason I am using the GET is because I can't get this stupid OpenURL to work the way they say it will (even tried it in VB just for kicks to no avail). I've searched the web and other sites say that the openURL method of the INET control can retreive contents this way as well. Anyone knows or figures it out please post back, thanks!


Slighthaze = NULL
 
<< The only reason I am using the GET is because I can't get this stupid OpenURL to work the way they say it will >>

I could not get OpenURL() to work in VFP, either.
So I am using code like yours to do the Execute().
I also needed to set _VFP.AutoYield = .F.
before the Execute().
This was:
1. For INET.StillExecuting to change to .T.
2 For INET.StateChanged() to work correctly.

- Andy Rice
San Diego, CA



Andy Rice
San Diego, CA
 
And the funny thing is that the &quot;incriminated&quot; code is executing correctly the FTP sequence and the data is transferred (I did a network capture). There is probably a bug in the implementation of the class. Maybe this wasn't intended to do ftp transfers.

 
csandu00

. Maybe this wasn't intended to do ftp transfers.

According to the paragraph at the begining of the article, it is.

The Internet Transfer control implements two widely-used Internet protocols: the HyperText Transfer Protocol (HTTP) and the File Transfer Protocol (FTP). Using the Internet Transfer control, you can connect to any site that uses one of these protocols, and retrieve files using either the OpenURL or Execute method.



Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
slighthaze,

Are you by chance going through a proxy server?


Important: If your proxy server is a CERN proxy server, you cannot make direct FTP connections by using the Execute method. In this case, to get a file, use the OpenURL method with the Open, Put, and Close statements. You can also use the OpenURL method to get a directory listing by invoking the method and specifying the target directory as the URL.


Dave S.
[cheers]
 
andyrice,
Thanks for the tip about _VFP.AutoYield = .F., I have been using DOEVENTS to effectuate the same thing. And, I am glad to see that someone else is having the same problem as I am...have been having a hard time finding out if anyone else sees this problem and whether the code I posted using the openURL method is working for anyone else.

Dave,

No Proxy Server, though I have code to handle proxy servers/authentication and such. I have begun to see a lot of the benefits of using the API calls instead, such as the ability to show accurate progress indication for FTP downloads and not having to make sure INET is registered on users' computers (though it is fairly standard these days).

Mike,

I am assuming by your last post that you are able to get the contents of a file off an FTP server by using the OpenURL method then? If so, I would appreciate your knowing what version of the INET control you are running as well as what code you are using to make this work. Thanks.

Slighthaze = NULL
 
One of the easiest ways to add download ability I have found is the following (though it doesn't work for my needs). This is a working example cut-n-paste into prg file and run.

Local sURLandFile, nRet
sURLandFile = &quot;ftp://ftp.microsoft.com/deskapps/readme.txt&quot;
Declare DoFileDownload IN shdocvw.dll STRING lpszFile
nRet=DoFileDownload(STRCONV(sURLandFile,5))



Slighthaze = NULL
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top