October 30, 2025, 05:53:04 PM

News:

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


Download a file from internet

Started by ExMember001, November 07, 2010, 04:41:56 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

ExMember001

Hi there,
i'm trying to download a file from my website server...
using some old code i have.. but i'm stuck there... seems like im rust a little bit
Can someone tell me why internetconnect() return me 0 ?


$MAIN

$ifndef WIN32
$define WIN32
$endif

$ifdef WIN32
$define WIN32_LEAN_AND_MEAN
$endif

$INCLUDE "windowssdk.inc"
$INCLUDE "winInet.inc"

CONST STATIC_1 = 1
CONST BUTTON_2 = 2
CONST idPG     = 3

CONST InitialVersion = 1.0

DIALOG updater
CREATEDIALOG updater,0,0,320,165,0x80C80080,0,"Vérification des mises à jours...",&handler_updater
CONTROL updater,@STATIC,"Placer un texte",25,33,271,21,0x50000101,STATIC_1
CONTROL updater,@SYSBUTTON,"Annuler",125,122,70,25,0x50010000,BUTTON_2

ProgressControl(updater,25,64,271,21,@BORDER|@PBS_SMOOTH,0,idPG)
SetProgressRange updater,idPG,0,100
SetProgressStep updater,idPG,1

SHOWDIALOG updater

DownloadFile("http://www.kcsoft.ca/version/unet.txt", "C:\\unet.txt")

WAITUNTIL updater = 0
END

SUB handler_updater
SELECT @MESSAGE
CASE @IDINITDIALOG
CENTERWINDOW updater
/* Initialize any controls here */
            SetProgressPosition(updater,idPG,75)

CASE @IDCLOSEWINDOW
CLOSEDIALOG updater,@IDOK
CASE @IDCONTROL
SELECT @CONTROLID
CASE BUTTON_2
IF @NOTIFYCODE = 0
/*button clicked*/
ENDIF
ENDSELECT
ENDSELECT
RETURN
ENDSUB


SUB DownloadFile(string urlfull, string Savepath),int
CONST bufferLength = 4096
INT isFile
    File urlfile
UINT hopen,hconnect,hhttp

STRING ref = ""

string site = GetDomainFromURL(urlfull)
string doc = GetDocFromURL(urlfull)

isFile = OpenFile( urlfile, Savepath,"W")
if isFile <> 1
hopen = InternetOpen(ref,0,"","",0)
IF hopen <> 0
            hconnect = InternetConnect(hopen,site,INTERNET_DEFAULT_HTTP_PORT,"","",INTERNET_SERVICE_HTTP,0,0)
            MESSAGEBOX(updater, str$(hconnect), "")
IF hconnect <> 0
                InternetCloseHandle(hconnect)
            endif
    InternetCloseHandle(hopen)
endif
    CLOSEFILE(urlfile)
ELSE
MESSAGEBOX(updater, "Erreur: Le fichier n'a pu être créé.", "Erreur")
return 0
endif
return 1
ENDSUB


SUB GetDomainFromURL(STRING URL),STRING
    STRING site
    INT n

site = URL

'remove "HTTP://" from the string
n = INSTR(UCASE$(site), "HTTP://")
IF n>0
site = RIGHT$(site, LEN(site)-7)
endif

'remove document info from the string
n = INSTR(UCASE$(site), "/")
IF n>0
    site = LEFT$(site,n-1)
endif

RETURN site
ENDSUB


SUB GetDocFromURL(STRING URL),STRING
STRING doc
INT n

doc = URL

'remove "HTTP://" from the string
n = INSTR(UCASE$(doc), "HTTP://")
IF n>0
doc = RIGHT$(doc, LEN(doc)-7)
endif

'remove site info from the string
n = INSTR(UCASE$(doc), "/")
IF n>0
doc = RIGHT$(doc, LEN(doc)-n)
ELSE
doc = ""
ENDIF

RETURN doc
ENDSUB

sapero

Hi Krypt,
InternetConnect fails because you have mixed INT with HINTERNET.
HINTERNET is defined as pointer (in wininet.inc) so "InternetConnect(hopen," will pass hopen by reference if you define hopen as int.
Always try using the additional types, not just INT:
SUB DownloadFile(string urlfull, string Savepath),int
CONST bufferLength = 4096
INT isFile
File urlfile
HINTERNET hopen,hconnect',hhttp

STRING ref = ""

string site = GetDomainFromURL(urlfull)
string doc = GetDocFromURL(urlfull)

isFile = OPENFILE( urlfile, Savepath,"W")
if isFile <> 1
hopen = InternetOpen(ref,INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0)
IF hopen <> 0
hconnect = InternetConnect(hopen,site,INTERNET_DEFAULT_HTTP_PORT,NULL,NULL,INTERNET_SERVICE_HTTP,0,0)
MESSAGEBOX(updater, STR$(hconnect), "InternetConnect")
IF hconnect <> 0
InternetCloseHandle(hconnect)
endif
InternetCloseHandle(hopen)
endif
CLOSEFILE(urlfile)
ELSE
MESSAGEBOX(updater, "Erreur: Le fichier n'a pu etre créé.", "Erreur")
return 0
endif
return 1
ENDSUB

ExMember001

look at that... didnt saw this one :)
thanks sapero