Download internet file without interference

Postby Patrick Mast » Mon Nov 17, 2008 5:36 pm

Hello,

Horizon wrote:Is it possible to show progress bar (for example in message bar) while reading at ReadAll() method?

I download big files from www. Users confuse if there is a problem or not.
Sorry, I don't know.

Patrick
User avatar
Patrick Mast
 
Posts: 246
Joined: Sat Mar 03, 2007 8:42 pm

Postby Horizon » Mon Nov 17, 2008 6:23 pm

Hi,

Where can i find the more documentation about TIpClientHttp. Is it from Windows Controls or someone writes it?

Thanks
Regards,

Hakan ONEMLI

Harbour & MSVC 2022 & FWH 23.04
Horizon
 
Posts: 1322
Joined: Fri May 23, 2008 1:33 pm

Postby Horizon » Tue Nov 18, 2008 6:59 pm

Hi,

I finally found the tip class in XHB source directory.

I have done some modifications in wfSaveURL function like below. It works perfectly with progressbar.



Code: Select all  Expand view
.
.
.
// oProg : Progress Bar is created before

cURL := "http://www.test.be/test.txt"
cSaveAs := "newfile.txt"
nFileSize := Get_File_Size(cURL)

IF wfReadURL(cURL, cSaveAs, oProg, nFileSize)
   ... File is there AND downloaded as cSaveAs
ELSE
   ... File is not there
ENDIF
.
.
.


FUNCTION wfSaveURL(cUrl, cSaveAs, oProg, nFileSize)
  LOCAL oConn, lReturn:=.T.
  DEFAULT oProg := 0
 
  IF Upper(Left(cUrl,4))#"HTTP"
     cUrl:="http://"+cUrl
  ENDIF
  TRY
   oConn := TipClientHttp():New(TURL():New(cUrl))
   oConn:nConnTimeout := 20000
   IF !EMPTY(oProg) .and. !EMPTY(nFileSize)
      oConn:exGauge := { | done, size| wfProgress(done, size, oProg ) }
      oProg:SetRange( 0, 99 )
   ENDIF

   IF oConn:Open(cURL)
      oConn:ReadToFile(cSaveAs,,nFileSize)
      oConn:Close()
   ENDIF
   
  CATCH
   lReturn := .F.
  END
 
RETURN lReturn

function wfProgress(nReceived,nTotalSize,ooProg)
ooProg:SetPos(INT(nReceived/nTotalSize*100)+1)
SysRefresh()
return

function Get_File_Size(cURL)
   LOCAL cBuffer, cRequest, cResponse, nBytes, pSocket, aRequest
   LOCAL crlf := CHR(13)+CHR(10), Result := 0, oUrl
   // initialize sockets system and connect to server
   DEFAULT cURL:=""
   oUrl := tURL():New( cUrl )
   IF Empty( oUrl )
     Return 0
   ENDIF
   IF Lower( oUrl:cProto ) <> "http"
     Return 0
   ENDIF
   
   INetInit()
   pSocket := INetConnect( oUrl:cServer, 80 )
   
   IF INetErrorCode( pSocket ) <> 0
      ? "Socket error:", INetErrorDesc( pSocket )
      INetCleanUp()
      RETURN 0
   ENDIF
   
   crlf := INETCRLF()
   
   // send HTTP request to server
   cRequest := "HEAD "+oUrl:BuildAddress() + " HTTP/1.1" + CRLF + ;
               "Host: "+oUrl:cServer+ CRLF + ;
               "User-Agent: HTTP-Get-File-Size" + CRLF + ;
               "Connection:close" + CRLF + ;
                CRLF
   nBytes   := INetSend( pSocket, cRequest )
   cBuffer  := Space(4096)
   cResponse:= ""
   
   // get HTTP response from server
   DO WHILE ( nBytes > 0 )
      nBytes    := INetRecv( pSocket, @cBuffer )
      cResponse += Left( cBuffer, nBytes )
      cBuffer   := Space(4096)
   ENDDO
   
   // disconnect and cleanup memory
   INetClose( pSocket )
   INetCleanUp()
   
   aRequest := HB_ATokens(cResponse, CRLF)
   clear
   nAt := Ascan(aRequest,{|x|LEFT(LTRIM(X),15)="Content-Length:"})
   IF nAt>0
      Result := VAL(SUBSTR(aRequest[nAt],17))
   ENDIF     
RETURN RESULT
Regards,

Hakan ONEMLI

Harbour & MSVC 2022 & FWH 23.04
Horizon
 
Posts: 1322
Joined: Fri May 23, 2008 1:33 pm

Postby RAMESHBABU » Wed Nov 19, 2008 4:09 am

Hello Mr.Horizon

A Small correction :

Code: Select all  Expand view
IF wfReadURL(cURL, cSaveAs, oProg, nFileSize)
   ... File is there AND downloaded as cSaveAs


should be

Code: Select all  Expand view
IF wfSaveURL(cURL, cSaveAs, oProg, nFileSize)
   ... File is there AND downloaded as cSaveAs


And oProg is an object. Is it not ? You have defined it as

Code: Select all  Expand view
DEFAULT oProg := 0


The :exGauge is not working with FWH. I don't know why...
And :SetRange and :SetPos methods are not there in TMETER
class of FWH. Instead :SetTotal and :Set methods are there. I changed
them as required in your source. Still the Progress bar is not seen.

Are you running this code in FWH or in terminal mode ?

>I finally found the tip class in XHB source directory.

One more request. May I get a copy of TIP CLASS SOURCE?

Thanks,

- Ramesh Babu P
User avatar
RAMESHBABU
 
Posts: 624
Joined: Fri Oct 21, 2005 5:54 am
Location: Secunderabad (T.S), India

Postby Horizon » Wed Nov 19, 2008 7:57 am

Hi Ramesh,

I use xHarbour Builder Sep. 2008 and FWH 8.10.

Code: Select all  Expand view
IF wfSaveURL(cURL, cSaveAs, oProg, nFileSize)
   ... File is there AND downloaded as cSaveAs


You are right. It should be.

Yes oProg is a Progress Bar object. like

Code: Select all  Expand view
@ 80, 010 PROGRESS oProg POSITION 0 SIZE 212, 12 PIXEL


Code: Select all  Expand view
DEFAULT oProg := 0


It might be DEFAULT oProg := NIL. It works now.

:exGauge works only :ReadtoFile() and :WriteToFile() methods.
You may use Progress instead of TMeter. :SetRange and :SetPos works now in Progress.

I will send a working example for you. It may help you.
Regards,

Hakan ONEMLI

Harbour & MSVC 2022 & FWH 23.04
Horizon
 
Posts: 1322
Joined: Fri May 23, 2008 1:33 pm

Postby Horizon » Wed Nov 19, 2008 8:28 am

It is the working example.

Code: Select all  Expand view

#include "FiveWin.ch"

//----------------------------------------------------------------------------//

function Main()
   local oDlg,  oBtnDwn, oBtnOk

   DEFINE DIALOG oDlg
@ 1, 1 BUTTON oBtnDwn PROMPT "Download" ACTION Download(oProg)
@ 2.5, 1 PROGRESS oProg POSITION 0 SIZE 100, 12
@ 3, 1 BUTTON oBtnOk PROMPT "Quit" ACTION oDlg:End()
   ACTIVATE DIALOG oDlg CENTER
   
return nil

PROCEDURE Download
LOCAL cURL, cSaveAs, nFileSize
cURL := "http://www.fivetechsoft.com/files/utilities/prometheus.exe"
cSaveAs := "prometh.exe"
nFileSize := Get_File_Size(cURL)

IF wfSaveURL(cURL, cSaveAs, oProg, nFileSize)
   ? "Downloaded" //   ... File is there AND downloaded as cSaveAs
ELSE
   ? "Can not downloaded " //... File is not there
ENDIF
RETURN


FUNCTION wfSaveURL(cUrl, cSaveAs, oProg, nFileSize)
  LOCAL oConn, lReturn:=.T.
  DEFAULT oProg := NIL
  DEFAULT nFileSize := 0
 
  IF Upper(Left(cUrl,4))#"HTTP"
     cUrl:="http://"+cUrl
  ENDIF
  TRY
   oConn := TipClientHttp():New(TURL():New(cUrl))
   oConn:nConnTimeout := 20000
   IF !EMPTY(oProg) .and. !EMPTY(nFileSize)
      oConn:exGauge := { | done, size| wfProgress(done, size, oProg ) }
      oProg:SetRange( 0, 99 )
   ENDIF

   IF oConn:Open(cURL)
      oConn:ReadToFile(cSaveAs,,nFileSize)
      oConn:Close()
   ENDIF
   
  CATCH
   lReturn := .F.
  END
 
RETURN lReturn

function wfProgress(nReceived,nTotalSize,ooProg)
ooProg:SetPos(INT(nReceived/nTotalSize*100)+1)
SysRefresh()
return

function Get_File_Size(cURL)
   LOCAL cBuffer, cRequest, cResponse, nBytes, pSocket, aRequest
   LOCAL crlf := CHR(13)+CHR(10), Result := 0, oUrl
   // initialize sockets system and connect to server
   DEFAULT cURL:=""
   oUrl := tURL():New( cUrl )
   IF Empty( oUrl )
     Return 0
   ENDIF
   IF Lower( oUrl:cProto ) <> "http"
     Return 0
   ENDIF
   
   INetInit()
   pSocket := INetConnect( oUrl:cServer, 80 )
   
   IF INetErrorCode( pSocket ) <> 0
      ? "Socket error:", INetErrorDesc( pSocket )
      INetCleanUp()
      RETURN 0
   ENDIF
   
   crlf := INETCRLF()
   
   // send HTTP request to server
   cRequest := "HEAD "+oUrl:BuildAddress() + " HTTP/1.1" + CRLF + ;
               "Host: "+oUrl:cServer+ CRLF + ;
               "User-Agent: HTTP-Get-File-Size" + CRLF + ;
               "Connection:close" + CRLF + ;
                CRLF
   nBytes   := INetSend( pSocket, cRequest )
   cBuffer  := Space(4096)
   cResponse:= ""
   
   // get HTTP response from server
   DO WHILE ( nBytes > 0 )
      nBytes    := INetRecv( pSocket, @cBuffer )
      cResponse += Left( cBuffer, nBytes )
      cBuffer   := Space(4096)
   ENDDO
   
   // disconnect and cleanup memory
   INetClose( pSocket )
   INetCleanUp()
   
   aRequest := HB_ATokens(cResponse, CRLF)
   clear
   nAt := Ascan(aRequest,{|x|LEFT(LTRIM(X),15)="Content-Length:"})
   IF nAt>0
      Result := VAL(SUBSTR(aRequest[nAt],17))
   ENDIF     
RETURN RESULT
Regards,

Hakan ONEMLI

Harbour & MSVC 2022 & FWH 23.04
Horizon
 
Posts: 1322
Joined: Fri May 23, 2008 1:33 pm

Re: Download internet file without interference

Postby Dave Zowasky » Fri May 01, 2009 1:47 pm

Hello All,

I am on Harbour 1.1.0dev (Rev. 10716)

I have hbtip.lib in my buildh.bat and all programs compile ok except when
I try to add turl code to my program.

If I add this to my program:

oConn := TipClientHttp():New(TURL():New(cUrl))


I get this error:

Error: Unresolved external '_pcre_config' referenced from H:\ZHARBOUR\LIB\HBRTL.LIB|hbregex
Error: Unresolved external '_pcre_stack_malloc' referenced from H:\ZHARBOUR\LIB\HBRTL.LIB|hbregex
Error: Unresolved external '_pcre_stack_free' referenced from H:\ZHARBOUR\LIB\HBRTL.LIB|hbregex


Any ideas?

Thanks
Dave Zowasky
Com1 Software, Inc.
http://www.com1software.com
information@com1software.com
330 653-3771
User avatar
Dave Zowasky
 
Posts: 125
Joined: Wed Oct 19, 2005 2:28 pm
Location: Hudson Ohio

Re: Download internet file without interference

Postby Antonio Linares » Sun May 03, 2009 12:30 am

Dave,

You have to link Harbour hbpcre.lib
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42082
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Download internet file without interference

Postby codemaker » Sun May 03, 2009 1:59 am

Was anybody able to run the example "horizon" posted?
For me it returns "Downloaded" but nothing wa sactualy downloaded.
User avatar
codemaker
 
Posts: 208
Joined: Wed Dec 03, 2008 4:48 pm
Location: Belgrade, Serbia

Re: Download internet file without interference

Postby Dave Zowasky » Sun May 03, 2009 1:47 pm

Antonio,

hbpcre.lib fixed my problem, however now getting:

HB_FUN_INETINIT'
HB_FUN_INETCONNECT
HB_FUN_INETERRORCODE
HB_FUN_INETERRORDESC

I have hbtip.lib linking in.
I thought hbtip.lib had these functions?

Thanks
Dave Zowasky
Com1 Software, Inc.
http://www.com1software.com
information@com1software.com
330 653-3771
User avatar
Dave Zowasky
 
Posts: 125
Joined: Wed Oct 19, 2005 2:28 pm
Location: Hudson Ohio

Re: Download internet file without interference

Postby Carles » Mon May 04, 2009 7:12 am

Hi !,

Can somebody send me the HBTIP.LIB for Harbour and their sources? I cna't connect to CVS... :(

Regards.
Salutacions, saludos, regards

"...programar es fácil, hacer programas es difícil..."

UT Page -> https://carles9000.github.io/
Forum UT -> https://discord.gg/bq8a9yGMWh
Skype -> https://join.skype.com/cnzQg3Kr1dnk
User avatar
Carles
 
Posts: 1134
Joined: Fri Feb 10, 2006 2:34 pm
Location: Barcelona

Re: Download internet file without interference

Postby Antonio Linares » Mon May 04, 2009 10:09 am

Dave,

You need to link XHB.lib
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42082
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Download internet file without interference

Postby Antonio Linares » Mon May 04, 2009 10:13 am

regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42082
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Download internet file without interference

Postby Carles » Mon May 04, 2009 5:29 pm

Antonio => Thanks a lot :D
Salutacions, saludos, regards

"...programar es fácil, hacer programas es difícil..."

UT Page -> https://carles9000.github.io/
Forum UT -> https://discord.gg/bq8a9yGMWh
Skype -> https://join.skype.com/cnzQg3Kr1dnk
User avatar
Carles
 
Posts: 1134
Joined: Fri Feb 10, 2006 2:34 pm
Location: Barcelona

Re: Download internet file without interference

Postby Dave Zowasky » Tue May 26, 2009 1:58 pm

Antonio,

Compiles great now, Thanks :D
Dave Zowasky
Com1 Software, Inc.
http://www.com1software.com
information@com1software.com
330 653-3771
User avatar
Dave Zowasky
 
Posts: 125
Joined: Wed Oct 19, 2005 2:28 pm
Location: Hudson Ohio

PreviousNext

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 71 guests