April 30, 2024, 09:37:43 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


downloding a file on a web sever

Started by chris, July 22, 2008, 05:27:12 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

chris

hey, i'm wondering if it is possible to get the contents of a file that is stored on a web sever using EBASIC? i am trying to make a program that lets you know if a web page has changed since the last time you checked so far the idea is to use php on the web server to opened the file and get a hash code then write the hash to a text file which is then ether downloaded and opened with EBASIC or opened right on the sever. my question is how to i get EBASIC to fetch a file that's not on the local computer?

LarryMc

If I was going to try it I would create a window then attach a browser to it then load the web page into that.

Look at the section in the help file labeled "Creating Embedded Browsers"

Some of the other members might know a "slicker" way.

Larry
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

chris

thanks for the info but that not quite what i want. what i want to end up with is the hash code in a string so i can work with it, unless i'm missing something  i don't see how that can be done by attaching a browser window anyother ideas?

chris

LarryMc

I guess I was thinking of "posting" form data from a web page which EB can do.

There's a lot of server stuff that people do.

Maybe Sapero can help you.

Sorry,

Larry

LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

chris

thanks for trying and i am glad i found that so i don't need to work out how to close IE from EBASIC now

LarryMc

Not sure what you mean but when you attach a brower window to EB you use the ATTACHBROWSER command and when you get through with it you just close the window you attached it to (which could be a child window of another window like I use it)

Larry
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

LarryMc

just thought of another way.

You can use the SYSTEM command in EB to run external programs (and if you use the API ShellExecuteExA you can run them hidden).

Then if you had an ftp program that would accept command line arguments then you could download the file that way.

Just brainstorming,

Larry
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

chris

well i'm going to tell the php script what to get the hash code of by useing GET because its shown in the url which means i can easly change what i send. i was thinking i would have to use the SYSTEM comand to open Internet Explorer and go to the webpage the problem was how do i then close Internet Explorer once i was done with it

and thanks for the FTP idea but i'd need to find ne i can give away that dosn't need any work to get up and running as i'm giveing out the program once it is ready if it was just for me i wouldn't bother with the EBASIC and just use the PHP it just takes awhile to set up and forward your own sever

Ionic Wind Support Team

Here is some code that I use a lot. 


$main
$use "version.lib"
$use "wininet.lib"
'API imports needed

DECLARE IMPORT,VerQueryValueA(pBlock as POINTER ,lpSubBlock as POINTER,lplpBuffer as POINTER,puLen as POINTER),INT
DECLARE IMPORT,GetFileVersionInfoSizeA(lptstrFilename as STRING,lpdwHandle as POINTER),UINT
DECLARE IMPORT,GetFileVersionInfoA(lptstrFilename as STRING,dwHandle as UINT,dwLen as UINT,lpData as POINTER),INT
DECLARE IMPORT,InternetOpenA(agent as string,access as int,proxyname as string,proxybypass as string,flags as int),int
DECLARE IMPORT,InternetConnectA(session as int,server as string,port as word,username as string,pass as string,service as int,flags as int,context as int),int
DECLARE IMPORT,InternetCloseHandle(handle as int),int
DECLARE IMPORT,HttpOpenRequestA(session as int,verb as string,name as string,version as pointer,referer as string,types as pointer,flags as int,context as int),int
DECLARE IMPORT,HttpSendRequestA(handle as int,headers as pointer,headerlength as int,option as pointer,optionlength as int),int
DECLARE IMPORT,InternetReadFile(hfile as int,buffer as pointer,count as int,bytesread as pointer),int
DECLARE IMPORT,InternetQueryDataAvailable(hfile as int,bytesavail as pointer,flags as int,context as int),int
DECLARE IMPORT,HttpQueryInfoA(hReq as UINT,dwInfo as UINT,lpvbuffer as pointer,lpdwlength as pointer,lpdwindex as pointer),INT
DECLARE IMPORT,InternetGetConnectedState(state as UINT BYREF,reserved as UINT),int
'TYPE needed
TYPE VS_FIXEDFILEINFO
  UINT dwSignature
  UINT dwStrucVersion
  UINT dwFileVersionMS
  UINT dwFileVersionLS
  UINT dwProductVersionMS
  UINT dwProductVersionLS
  UINT dwFileFlagsMask
  UINT dwFileFlags
  UINT dwFileOS
  UINT dwFileType
  UINT dwFileSubtype
  UINT dwFileDateMS
  UINT dwFileDateLS
ENDTYPE
'constants
SETID "INTERNET_SERVICE_FTP",1
SETID "INTERNET_SERVICE_GOPHER",2
SETID "INTERNET_SERVICE_HTTP",3

SETID "INTERNET_PRECONFIG",0
SETID "INTERNET_DIRECT",1
SETID "INTERNET_PROXY",3
SETID "INTERNET_FLAG_RELOAD",0x80000000
SETID "INTERNET_FLAG_NO_CACHE_WRITE",0x04000000
SETID "HTTP_QUERY_CONTENT_LENGTH",5
SETID "HTTP_QUERY_STATUS_CODE",19
SETID "HTTP_QUERY_FLAG_NUMBER",0x20000000

'template for our progress callback
declare progresscb(uint totalsize,uint current)


'gets a file from the server and saves it to disk.
SUB DownloadToFile(server as string,remote as string,local as string,progCallback as UINT),int
STRING referrer:referrer = "EBasic Client"
char buffer[1024]
UINT hOpen,hconnect,hhttp,dwSize,dwSizeLen,dwCurrent,statuscode
BFILE fSave
int br:br = 0
int success:success = FALSE
string _temp:_temp=getstartpath+"__"+local
hopen = InternetOpenA(referrer,@INTERNET_PRECONFIG,"","",0)
if(hopen)
hconnect = InternetConnectA(hopen,server,80,"","",@INTERNET_SERVICE_HTTP,0,0)
if(hconnect)
hhttp = HttpOpenRequestA(hconnect,"GET",remote,null,"",null,@INTERNET_FLAG_RELOAD|@INTERNET_FLAG_NO_CACHE_WRITE,0)
if(hhttp)
IF HttpSendRequestA(hhttp,null,0,null,0)
'get the size of the remote file, if available
dwSizeLen = 4
dwSize = 0
dwCurrent = 0
HttpQueryInfoA(hhttp,@HTTP_QUERY_CONTENT_LENGTH|@HTTP_QUERY_FLAG_NUMBER,dwSize,dwSizeLen,NULL)
HttpQueryInfoA(hhttp,@HTTP_QUERY_STATUS_CODE|@HTTP_QUERY_FLAG_NUMBER,statuscode,dwSizeLen,NULL)
'download and store the file
' only attempt to download the file if it really exists and is available
' otherwise the buffer contains an html document with "404 not found, 403 server error, etc".
if statuscode = 200
if(OpenFile(fSave,_temp,"W") = 0)
DO
InternetReadFile(hhttp,buffer,1024,br)
if br
dwCurrent += br
__write(fSave,buffer,br)
if(progCallback)
!<progresscb>progCallback(dwSize,dwCurrent)
endif
endif
UNTIL br = 0 or CancelDownload=true
CloseFile(fSave)
if CancelDownload=FALSE
CopyFile(_temp, getstartpath+local, 0)
endif
DeleteFile(_temp)
if CancelDownload=TRUE then success=FALSE else success = TRUE
endif
endif
else
MessageBox update,"Cannot connect to server\nPlease check your internet connection and try again","Error",@MB_ICONEXCLAMATION
bStart = FALSE
CloseWindow update
ENDIF
InternetCloseHandle(hhttp)
endif
InternetCloseHandle(hconnect)
endif
InternetCloseHandle(hopen)
endif
RETURN success
ENDSUB
Ionic Wind Support Team

chris

wow... just wow that’s going to take awhile to work out but thanks  showing me it can work here i was hoping for something simple :) but i'll take what i can get thanks chris

chris

okay i tryed to compile the code as you gave it but i got a wack of errors

Compiling...
downlodeFile.eba
File: C:\Program Files\EBDev\projects\Database samples\downlodeFile.eba (88) undefined variable - CancelDownload
File: C:\Program Files\EBDev\projects\Database samples\downlodeFile.eba (90) undefined variable - CancelDownload
File: C:\Program Files\EBDev\projects\Database samples\downlodeFile.eba (94) undefined variable - CancelDownload
File: C:\Program Files\EBDev\projects\Database samples\downlodeFile.eba (98) undefined variable - update
File: C:\Program Files\EBDev\projects\Database samples\downlodeFile.eba (98) invalid parameter
File: C:\Program Files\EBDev\projects\Database samples\downlodeFile.eba (100) undefined variable - update
File: C:\Program Files\EBDev\projects\Database samples\downlodeFile.eba (100) no appropriate conversion exists
Error(s) in compiling "C:\Program Files\EBDev\projects\Database samples\downlodeFile.eba"
any ideas on what i have to do to fix it? and while i'm at it how do i tell it what file to get and what sever to get it from?

Ionic Wind Support Team

Well there are easier ways,this one just has the most flexibility since you can check the status code of the remote file,   304 means not modified.

Currently it is set up to always get the remote file, and ignore any locally cached copies.  Which is necessary if you are retrieving a file that changes frequently.

You may need to do some research on the functions, use google ;).

Paul.

Ionic Wind Support Team

Ionic Wind Support Team

It's not meant to compile directly, just ripped out of one of my programs.  CancelDownload is just defined as an INT and my progress dialog has a "Cancel" button that sets it to 1.  Just define it as an INT somewhere in your program, and set it to 0.

"update" was the window variable in my program.  Just comment the line out.

server = "http://www.yourwebsite.com"
remote = "downloads/myfile.whatever"

I can't tell you the server and remote, you are the one that knows that ;)
Ionic Wind Support Team

chris

thansk it compiles now but i'm still not getting a file saved  where i want it is local there varable that tells the program where to save the file?

Ionic Wind Support Team

the local parameter of the function is where you specify where the file gets saved: ie

DownloadToFile("http://www.test.com","file.txt","c:\\myfiles\\file.ext",NULL)

Ionic Wind Support Team

chris

okay so i added this to the end of the code and its still not working i'm sure that i've got the right information so what am i doing wrong?

IF DownloadToFile("http://www.authorskeep.com/","/home/authorsk/public_html/fallen/index.php","C:\\Users\\chris\\Downloads\\index.php",NULL)
PRINT "It worked"
ENDIF
DO : UNTIL INKEY$<>""

Ionic Wind Support Team

chris,
try:

DownloadToFile("http://www.authorskeep.com","index.php","C:\\Users\\chris\\Downloads\\index.php",NULL)

The full server path isn't remotely accessable, only whatever is off of public_html, hence the name.

Why are you trying to download the index.php file? 

Paul.
Ionic Wind Support Team

chris

July 24, 2008, 05:58:16 PM #17 Last Edit: July 24, 2008, 06:02:09 PM by chris
okay i tryed useing
DownloadToFile("http://www.authorskeep.com","index.php","C:\\Users\\chris\\Downloads\\index.php",NULL)
but no luck is there anything on the server side that needs to set?
P.S i'm trying to get index.php to see if it works no other reson

Ionic Wind Support Team

Forgot....don't use the http:

DownloadToFile("www.authorskeep.com","index.php","C:\\Users\\chris\\Downloads\\index.php",NULL)

It's just the servers address, or IP address.   

if .php files don't work then try a simple text file to experiment with.
Ionic Wind Support Team

chris

allright its still not working anything else to try? or is there anyother way to get data off of a sever?

Ionic Wind Support Team

I use that subroutine in a production environment to update frequently changed client files, multi megabytes worth, never fails. Have never tried it with a .php file which is why I suggested to try a .txt file to experiment with.  Most likely server security settings are preventing a direct GET of files with a .php extension.

Insert some print statements to see where it is failing for you. Do some debugging, it is what programming is all about.

Quote
is there anyother way to get data off of a sever?

Probably dozens, this is the easiest way I know of which allows bypassing the IE cache so you can get to a file that might have changed since the last time you read it.  You could use winsock direclty, but that would require pages of code.

See the test program internet_example.eba that comes with the installation for a slightly scaled down version of the provided subroutine. 

Paul.
Ionic Wind Support Team

chris

thanks for the help i'm just glad that it can be done i'll kep working on it then

yours chris

LarryMc

Chris
Did you ever get it working for you?

If you didn't I can help.


Larry
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

LarryMc

Paul,
Is there a way to make your routine work with a "generated" page?

The call to the sub would look like this:
DownloadToFile("www.blah.com","isapi.dll?c=c",getstarpath+"tmp.txt",NULL)

Larry
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Ionic Wind Support Team

No idea, have you tried it?

The routine was designed specifically to download files for a site, executables, zips, images, etc.

Paul.
Ionic Wind Support Team