May 21, 2024, 07:52:16 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


getting a file off a server

Started by chris, August 04, 2008, 10:31:01 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

chris

okay time to post this question here. i'm trying to download a file off a sever useing this code
$main
$use "version.lib"
$use "wininet.lib"
'API imports needed
DEF CancelDownload : int
CancelDownload = 0
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)
PRINT "hhttp works"
IF HttpSendRequestA(hhttp,null,0,null,0)
PRINT "line 68 worked"
'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, local, 0)
endif
DeleteFile(_temp)
if CancelDownload=TRUE then success=FALSE else success = TRUE
endif
endif
else
PRINT " it didn't work"
'MessageBox update,"Cannot connect to server\nPlease check your internet connection and try again","Error",@MB_ICONEXCLAMATION
'bStart = FALSE
'CloseWindow update
ENDIF
ELSE
PRINT " error on line 78"
InternetCloseHandle(hhttp)
ELSE
PRINT "eror on line 66 70"
endif
InternetCloseHandle(hconnect)
ELSE
PRINT "erro on line 65"
endif
InternetCloseHandle(hopen)
ELSE
PRINT "eror in line 63"
endif
RETURN success
ENDSUB

IF DownloadToFile("www.authorskeep.com/","test.txt","C:\\Users\\chris\\Downloads\\index.txt",NULL)
PRINT "It worked"
ENDIF
DO : UNTIL INKEY$<>""

the line that is giving me trouble is
IF HttpSendRequestA(hhttp,null,0,null,0)
i am sure that the file path both to  the server and on my local machine are correct and i've tried both .php and .html before trying .txt so i don't think it's the file extention if there is another way to get the file i'm willing to try, the file will be no bigger then 1kb
by the way i'm useing EBasic just in case that was not clear
thanks for all of the help
chris

LarryMc

okay Chris
I'll work with you a little while right now.
While I'm copying and setting up to run the program, try this:
IF DownloadToFile("www.authorskeep.com","test.txt","C:\\Users\\chris\\Downloads\\index.txt",NULL)
PRINT "It worked"
ENDIF


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

okay what ever you did that bit of code work ... mostly
now my problem is with this part
if(OpenFile(fSave,_temp,"W") = 0)
PRINT  "open file works"
DO
InternetReadFile(hhttp,buffer,1024,br)
if br
dwCurrent += br
__write(fSave,buffer,br)
if(progCallback)
!<progresscb>progCallback(dwSize,dwCurrent)
endif
endif

i realy hope this is all worth it. it's driving me nutts

LarryMc

I'm working with the openfile problem already.

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

replace your corresponding code with this:
there were several things changed including some else statements.

string _temp:_temp=local
hopen = InternetOpenA(referrer,@INTERNET_PRECONFIG,"","",0)
if(hopen)
hconnect = InternetConnectA(hopen,server,80,"","",@INTERNET_SERVICE_HTTP,0,0)
if(hconnect)
print "connect"
hhttp = HttpOpenRequestA(hconnect,"GET",remote,null,"",null,@INTERNET_FLAG_RELOAD|@INTERNET_FLAG_NO_CACHE_WRITE,0)
print hhttp
'hhttp = HttpOpenRequestA(hconnect,"GET",remote,null,"",null,@INTERNET_FLAG_RELOAD|@INTERNET_FLAG_NO_CACHE_WRITE,0)
if(hhttp)
PRINT "hhttp works"
'IF HttpSendRequestA(hhttp,null,0,null,0)
            IF HttpSendRequestA(hhttp,null,0,null,0)
PRINT "line 68 worked"
'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, local, 0)
'endif
'DeleteFile(_temp)
if CancelDownload=TRUE then success=FALSE else success = TRUE
endif
else
print "file didn't open"
endif
else
    PRINT " it didn't work"
'MessageBox update,"Cannot connect to server\nPlease check your internet connection and try again","Error",@MB_ICONEXCLAMATION
'bStart = FALSE
'CloseWindow update
ENDIF
InternetCloseHandle(hhttp)
ELSE
PRINT "eror on line 66 70"
endif
InternetCloseHandle(hconnect)
ELSE
PRINT "erro on line 65"
endif
InternetCloseHandle(hopen)
ELSE
PRINT "eror in line 63"
endif
RETURN success
ENDSUB

IF DownloadToFile("www.authorskeep.com","hosted/chris_fallen/index.php","C:\\\\Users\\Chris\\Downloads\\index.txt",NULL)
PRINT "It worked"
ENDIF
print :print "press key"
DO : UNTIL INKEY$<>""

I haven't been able to make the test.txt file read.
It's in the root dir so that might be the problem.
In my browser it reads this is a test.
Running it through the program it looks like a pure binary file.

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

I take that back; it will read the test.txt file.

I was basing my comment on the fact that it looked like a binary file while I was trapping it in a sort of debug mode.

But when I got rid of all my debug stuff it read test.txt just fine.

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

jsanders

Hello Chris,

I'll step in here with more of an explanation.

You were passing "C:\\Users\\chris\\Downloads\\index.txt" into the local parameter.

And then doing this:

string _temp:_temp=getstartpath+"__"+local

Which would make an invalid path.  For example if the executable was located in c:\\myprograms then _temp would contain:

c:\\myprograms\\C:\\Users\\chris\\Downloads\\index.txt

Regards,
James Sanders.

P.S...Larry beat me to it.

chris

IT WORKS!!!! sorry for that but i've been slaving over  this  all  week thnak you for all the help now to get the php bit set up at least that i understand.

thanks again
chris

LarryMc

Quote from: chris on August 04, 2008, 11:28:37 PM
...now to get the php bit set up at least that i understand.
I'm glad you do because I sure don't.

glad I could help a little.

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