Download file from WWW

HMG Samples and Enhancements

Moderator: Rathinagiri

User avatar
mol
Posts: 3718
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Download file from WWW

Post 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
Attachments
download.zip
(2.26 KiB) Downloaded 607 times
User avatar
Vanguarda
Posts: 543
Joined: Wed Feb 11, 2009 10:56 am
Location: Americana - SP
Contact:

Re: Download file from WWW

Post 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,
--
Paulo Sérgio Durço (Vanguarda)


http://hmglights.wordpress.com/
jouhmg
Posts: 26
Joined: Thu Jul 31, 2008 3:22 am

Re: Download file from WWW

Post by jouhmg »

Hi
I link that library to test this demo?
Regards
User avatar
fchirico
Posts: 324
Joined: Sat Aug 23, 2008 11:27 pm
Location: Argentina

Re: Download file from WWW

Post 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.
Saludos, Fernando Chirico.
User avatar
mol
Posts: 3718
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: Download file from WWW

Post 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
User avatar
Vanguarda
Posts: 543
Joined: Wed Feb 11, 2009 10:56 am
Location: Americana - SP
Contact:

Re: Download file from WWW

Post 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,
--
Paulo Sérgio Durço (Vanguarda)


http://hmglights.wordpress.com/
User avatar
mol
Posts: 3718
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: Download file from WWW

Post 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
Attachments
download_with_proxy.zip
(33.45 KiB) Downloaded 534 times
User avatar
Rathinagiri
Posts: 5471
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Contact:

Re: Download file from WWW

Post by Rathinagiri »

Thank you very much Marek. Just downloaded and testing. :)
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.
User avatar
Vanguarda
Posts: 543
Joined: Wed Feb 11, 2009 10:56 am
Location: Americana - SP
Contact:

Re: Download file from WWW

Post by Vanguarda »

Hi all,

Thank marek for this nice work!



regards,
--
Paulo Sérgio Durço (Vanguarda)


http://hmglights.wordpress.com/
APi
Posts: 6
Joined: Tue Dec 29, 2015 3:34 pm
Location: Germany (born in Austria)

Re: Download file from WWW

Post by APi »

Hello Marek,
you have a wonderful motorcycle. Thank you for your work.
Greeting from APi
Last edited by APi on Wed Dec 28, 2016 12:42 pm, edited 1 time in total.
Post Reply