Page 1 of 5

Download file from WWW

Posted: Sun Jun 21, 2009 2:08 pm
by mol
Success!!!
Two days of searching ant testing and I've compiled from everything I found, small function that is really BIG!
I wanna share it with us, maybe it will be useful for somebody!
I 've prepared working example with this function. Test and have fun!


Code: Select all

PROCEDURE DownloadFromWWW
   param cURL, cLocalFileName
   LOCAL oCon, oUrl, i

   cLocalFileName := alltrim(cLocalFileName)
   
   oUrl := tURL():New( cUrl )
   IF Empty( oUrl )
      MsgBox("Invalid url " + cUrl)
      return
   ENDIF

   IF oUrl:cProto != "http"
      MsgBox('This is a header test for http. Use an http address.')
      return
   END
   
   oCon := TipClientHttp():New( oUrl )
   oCon:nConnTimeout := 20000
   MsgBox("Connecting with "+ oUrl:cServer)
   IF oCon:Open( cUrl )

   MsgBox("Connection eshtablished." +chr(10)+"Press OK to retrieve" + oUrl:cPath +oUrl:cFile)
   oCon:WriteAll(cLocalFileName)
   MsgBox("Downloaded...")
    oCon:Close()
   ELSE
      MsgBox("Can't connect with "+ oUrl:cServer)
      IF oCon:SocketCon == NIL
         MsgBox("Connection not initiated")
      ELSEIF hb_InetErrorCode( oCon:SocketCon ) == 0
         MsgBox("Server sayed: "+ oCon:cReply)
      ELSE
         MsgBox("Error in connection: " + hb_InetErrorDesc( oCon:SocketCon ))
      ENDIF
   END
RETURN

Re: Download file from WWW

Posted: Sun Jun 21, 2009 4:54 pm
by Vanguarda
Hi MOL,

You are a great programmer.
I like very much. I think it is very useful for many purpose.
Thank for sharing this wonderful function with us.


with best regards,

Re: Download file from WWW

Posted: Mon Jun 22, 2009 3:33 am
by jouhmg
Hi
I link that library to test this demo?
Regards

Re: Download file from WWW

Posted: Mon Jun 22, 2009 3:36 pm
by fchirico
jouhmg wrote:Hi
I link that library to test this demo?
Regards
I'm compiling with HMG 2.9.2 and IDE 2.8.6g and not need nothing more.

Saludos, Fernando Chirico.

Re: Download file from WWW

Posted: Mon Jun 22, 2009 6:59 pm
by mol
program uses functions from hbtip library, which is included in HMG.

I'm working now on the function which will allow to download via proxy server.
I will share my work, when I finish.

Marek

Re: Download file from WWW

Posted: Tue Jun 23, 2009 1:12 am
by Vanguarda
mol wrote:program uses functions from hbtip library, which is included in HMG.

I'm working now on the function which will allow to download via proxy server.
I will share my work, when I finish.

Marek
Great work MOL


with best regards,

Re: Download file from WWW

Posted: Tue Jun 23, 2009 8:00 am
by mol
I finished!

Version with downloading via proxy server is now complete!
I'm putting whole HMG example.
Please, download and test!
I will be graceful for your opinions!
PS.
Remember to add lib SOCKET in HMG preferences!

Code: Select all

FUNCTION DownloadFromWWWviaProxy
	param cServer, cURL, cLocalFileName, cProxy, nProxyPort, cProxyUser, cProxyPassword

	private oSock, cRet
	private nPort    := 80               // 8080         //

	DEFAULT nProxyPort := 8080

	cServer := substr(cServer, at("http://",cServer) +7 )
	
	oSock := THttp():New()
	
	if !empty(cProxy)
		oSock:SetProxy( cProxy, nProxyPort, cProxyUser, cProxyPassword )
	endif
	delete file &cLocalFileName
	MsgBox("Trying tonnect to " +cServer +":" +alltrim(str( nPort )) )
	if oSock:Connect( cServer, nPort )
		MsgBox("Connected. Trying to download file")
		cAnswer := oSock:Get( cServer+cUrl)

		// remove HTTP header
		nPos := at(chr(13)+chr(10)+chr(13)+chr(10), cAnswer)
		cHeader := left(cAnswer, nPos+4)
		// we must cut off HTTP header
		cAnswer := substr(cAnswer,nPos+4)

		// search for file length
		nPos := at("Content-Length:",cHeader)
		if nPos > 0
			nLengthOfFile := val(substr(cHeader,nPos+len("Content-Length:")))
		else
			// something wrong
			nLengthOfFile := 0
		endif
		if nLengthOfFile <> len(cAnswer)
			// something was wrong
			MsgBox("something was wrong " + cHeader)
		else
			nFile := FCreate(cLocalFileName,0)
			FWrite(nFile, cAnswer)
			FClose(nFile)
		endif

		// Trying to close connection
		if oSock:Close()
			MsgBox("Close connection successfull")
		else
			MsgBox("Error on close connection")
		endif
    else
		MsgBox("Connection refused")
	endif

 RETURN NIL

Re: Download file from WWW

Posted: Tue Jun 23, 2009 9:27 am
by Rathinagiri
Thank you very much Marek. Just downloaded and testing. :)

Re: Download file from WWW

Posted: Tue Jun 23, 2009 9:14 pm
by Vanguarda
Hi all,

Thank marek for this nice work!



regards,

Re: Download file from WWW

Posted: Tue Dec 27, 2016 7:23 pm
by APi
Hello Marek,
you have a wonderful motorcycle. Thank you for your work.
Greeting from APi