April 19, 2024, 01:02:26 AM

News:

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


downloading a password protected file on the internet

Started by philippe.tx, January 06, 2011, 06:35:27 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

philippe.tx

hi all,
i would like to download a file from the internet.
the site is password protected and i must use a url like http://my_email_adress@my_provider.fr:my_password@site.com/ranking.php?idr=123

using a browser i can get that file.

the trouble is that when i try to download that file in a program using wininet or URLDownloadToFile it doesn't work.
i suspect the @ in the email adress is guilty but it could be anything esle.
the progs work fine with any url containing only 1 @.
any ideas? ???

GWS

Not something I've ever tried Philippe, but maybe this info might help ..  :)

First from Sapero (it's EBasic code but could be modified for CBasic ..


$use "Wininet.lib"
$use "Urlmon.lib"
declare import, URLDownloadToFileA(comref pCaller,string szURL,string szFileName,_
int dwReserved, comref lpfnCB),int
declare import, DeleteUrlCacheEntryA(string szURL)

' optional, remove the file from browser's cache
DeleteUrlCacheEntryA("http://www.google.com/intl/en_ALL/images/logo.gif")

' then download it
if URLDownloadToFileA(0, "http://www.google.com/intl/en_ALL/images/logo.gif", "c:\\image.gif", 0, 0)
Messagebox 0, "download failed", ""
else
system "c:\\image.gif"
endif


Notice the function used is URLDownloadToFileA

Second, some work Joske did (also in EBasic Code) ..

http://www.codingmonkeys.com/index.php?topic=677.0

Hope it helps, :)

Graham
Tomorrow may be too late ..

philippe.tx

hi gws,
thanx for the answer.
unfortunately the codes you provide are the two i've tried and failed to ;D
sapero's code:
it doesn't work and i have absolutely no clue why. and it's a pity as it is so concise and clear.
joske's code using wininet:
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

DECLARE "wininet",InternetOpenA(agent:string,access:int,proxyname:string,proxybypass:string,flags:int),int
DECLARE "wininet",InternetConnectA(session:int,server:string,port:word,username:string,pass:string,service:int,flags:int,context:int),int
DECLARE "wininet",InternetCloseHandle(handle:int),int
DECLARE "wininet",HttpOpenRequestA(session:int,verb:string,name:string,version:pointer,referer:string,types:pointer,flags:int,context:int),int
DECLARE "wininet",HttpSendRequestA(handle:int,headers:pointer,headerlength:int,option:pointer,optionlength:int),int
DECLARE "wininet",InternetReadFile(hfile:int,buffer:string,count:int,bytesread:pointer),int
DECLARE "wininet",InternetQueryDataAvailable(hfile:int,bytesavail:pointer,flags:int,context:int),int

DEF hopen,hconnect,hhttp:int
DEF buffer:STRING
DEF br,avail:int
DEF null:POINTER
REM change the site$ to any site you want.

site$ = "http://virtual-loup-de-mer.org"
doc$="/ws/raceinfo/ranking.php?idr=20101231"

OPENCONSOLE

hopen = InternetOpenA("vlm2kml",@INTERNET_PRECONFIG,"","",0)
IF hopen
PRINT " 1           Wininet initialized"
hconnect = InternetConnectA(hopen,site$,80,"my_email_adress","my_password",@INTERNET_SERVICE_HTTP,0,0)
IF hconnect
PRINT "2         Connection established"
hhttp = HttpOpenRequestA(hconnect,"GET",doc$,null,"",null,@INTERNET_FLAG_RELOAD,0)
IF hhttp
print "3"
IF HttpSendRequestA(hhttp,null,0,null,0)
PRINT "4          Request Made"
DO
InternetReadFile(hhttp,buffer,255,br)
buffer = left$( buffer,br)
IF br THEN PRINT buffer,
buffer = ""
UNTIL br = 0
ENDIF
InternetCloseHandle(hhttp)
ENDIF
InternetCloseHandle(hconnect)
ENDIF
InternetCloseHandle(hopen)
ENDIF

PRINT
PRINT "Press any key to close"
DO
UNTIL INKEY$ <> ""
CLOSECONSOLE




hopen = InternetOpenA("vlm2kml",@INTERNET_PRECONFIG,"","",0) <-- this works
IF hopen
PRINT " 1           Wininet initialized"<--printed
hconnect = InternetConnectA(hopen,site$,80,"my_email_adress","my_password",@INTERNET_SERVICE_HTTP,0,0)<--this works
IF hconnect
PRINT "2         Connection established"<--printed
hhttp = HttpOpenRequestA(hconnect,"GET",doc$,null,"",null,@INTERNET_FLAG_RELOAD,0)<--this SEEMS to work
IF hhttp
print "3"<--printed
  IF HttpSendRequestA(hhttp,null,0,null,0)<--this doesn't work
PRINT "4          Request Made"<--not printed

i suppose that the 4th request doesn't work because the 3rd is not the good one.

in http://msdn.microsoft.com/en-us/library/aa384233(VS.85).aspx i've tried to understand each parameter.
HINTERNET HttpOpenRequest(
  __in  HINTERNET hConnect,
  __in  LPCTSTR lpszVerb,
  __in  LPCTSTR lpszObjectName,
  __in  LPCTSTR lpszVersion,
  __in  LPCTSTR lpszReferer,
  __in  LPCTSTR *lplpszAcceptTypes,
  __in  DWORD dwFlags,
  __in  DWORD_PTR dwContext
);
with

lplpszAcceptTypes [in]
A pointer to a null-terminated array of strings that indicates media types accepted by the client. If this parameter is NULL, no types are accepted by the client. Servers generally interpret a lack of accept types to indicate that the client accepts only documents of type "text/*" (that is, only text documentsââ,¬â€no pictures or other binary files). For more information and a list of valid media types, see ftp://ftp.isi.edu/in-notes/iana/assignments/media-types/media-types.

the only thing i can think of, is that i should tell that i want a php file and not a text file.
but i don't know how.
of course i'm certainly completely wrong in my interpretations  ;D



sapero

You can't pass protocol name ( http: ) to InternetConnect, it must be a host name "www.a.b" or IP number string.
The array of pointers should be:
pointer[0] -> "text/html"
pointer[1] -> "image/gif"
...
pointer[n] -> NULL


I have two new examples, but can't test if they are working, and there is an syntax error which I can't localize ;)

declare "urlmon.dll", UrlMkGetSessionOption(dwOption:UINT, pBuffer:string, dwBufferLength:UINT, pdwBufferLengthOut:UINT ByRef, dwReserved:UINT),INT
declare "Wininet.dll", InternetOpenA(lpszAgent:string, dwAccessType:UINT, lpszProxy:pointer, lpszProxyBypass:pointer, dwFlags:UINT),int
declare "Wininet.dll", InternetOpenUrlA(hInternet:int, lpszUrl:string, lpszHeaders:pointer, dwHeadersLength:UINT, dwFlags:UINT, dwContext:UINT),int
declare "Wininet.dll", InternetSetOptionA(hInternet:int, dwOption:UINT, lpBuffer:string, dwBufferLength:UINT),INT
declare "Wininet.dll", InternetReadFile(hFile:int, lpBuffer:string, dwNumberOfBytesToRead:UINT, lpdwNumberOfBytesRead:UINT ByRef),INT
declare "Wininet.dll", InternetCloseHandle(hInternet:int),INT
declare "Wininet.dll", InternetConnectA(hInternet:int, lpszServerName:string, nServerPort:word, lpszUserName:pointer, lpszPassword:pointer, dwService:UINT, dwFlags:UINT, dwContext:UINT),int
declare "Wininet.dll", HttpOpenRequestA(hConnect:int, lpszVerb:string, lpszObjectName:string, lpszVersion:pointer, lpszReferrer:pointer, lplpszAcceptTypes:pointer, dwFlags:UINT, dwContext:UINT),int
declare "Wininet.dll", HttpSendRequestA(hRequest:int, lpszHeaders:pointer, dwHeadersLength:UINT, lpOptional:pointer, dwOptionalLength:UINT),INT
declare "kernel32.dll",WriteFile(hFile:BFILE, lpBuffer:pointer,nNumberOfBytesToWrite:UINT, lpNumberOfBytesWritten:UINT ByRef, lpOverlapped:pointer),int

const URLMON_OPTION_USERAGENT = 0x10000001
const INTERNET_OPEN_TYPE_PRECONFIG = 0
const INTERNET_OPTION_USERNAME = 28
const INTERNET_OPTION_PASSWORD = 29
const INTERNET_INVALID_PORT_NUMBER = 0
const INTERNET_SERVICE_HTTP = 3
const INTERNET_FLAG_KEEP_CONNECTION = 0x00400000

declare DownloadToFile1(hTargetFile:BFILE, szUrl:string, user:string, password:string),int
declare DownloadToFile2(hTargetFile:BFILE, szHostName:string, szResource:string, user:string, password:string),INT


' 1. open target file in binary mode
' 2. call DownloadToFile1 or DownloadToFile2. Returns the number of downloaded bytes.
' 3. close the file

sub DownloadToFile1(hTargetFile, szUrl, user, password)

' hTargetFile - file opened by CreateFile, OPENFILE
def size,hInternet,hFile,cchUser,cchPass:int
def dwNumberOfBytesRead, dwNumberOfBytesWriten:UINT
def buffer[1024]:istring

size = 0

if (UrlMkGetSessionOption(URLMON_OPTION_USERAGENT, buffer, 1024, size, 0)<0)
buffer = "my user agent"
endif

hInternet = InternetOpenA(buffer,INTERNET_OPEN_TYPE_PRECONFIG,0,0,0)
if (hInternet)

hFile = InternetOpenUrlA(hInternet,szUrl,0,0,0,0)
if (hFile)

cchUser = len(user)
cchPass = len(password)

if (cchUser and cchPass)

InternetSetOptionA(hFile, INTERNET_OPTION_USERNAME, user, cchUser+1)
InternetSetOptionA(hFile, INTERNET_OPTION_PASSWORD, password, cchPass+1)
endif

while (InternetReadFile(hFile,buffer,1024,dwNumberOfBytesRead) and dwNumberOfBytesRead)

WriteFile(hTargetFile, buffer, dwNumberOfBytesRead, &dwNumberOfBytesWriten, 0)
size = size + dwNumberOfBytesWriten
endwhile
InternetCloseHandle(hFile)
endif
InternetCloseHandle(hInternet)
endif
return size



sub DownloadToFile2(hTargetFile, szHostName, szResource, user, password)
' hTargetFile - file opened by CreateFile, OPENFILE
' szHostName  - www.site.com
' szResource  - relative path to file: /premium/default.htm
def size,hOpenHandle,hConnectHandle,hResourceHandle,cchUser,cchPass : int
def buffer[1024] : istring

size = 0

if (UrlMkGetSessionOption(URLMON_OPTION_USERAGENT, buffer, 1024, size, 0) < 0)
buffer = "my user agent"
endif

hOpenHandle = InternetOpenA(buffer, INTERNET_OPEN_TYPE_PRECONFIG,0,0,0)
if (hOpenHandle)

hConnectHandle = InternetConnectA(hOpenHandle,szHostName,INTERNET_INVALID_PORT_NUMBER,0,0,INTERNET_SERVICE_HTTP,0,0)
if (hConnectHandle)

hResourceHandle = HttpOpenRequestA(hConnectHandle,"GET",szResource,0, 0, 0, INTERNET_FLAG_KEEP_CONNECTION, 0)
if (hResourceHandle)

cchUser = len(user)
cchPass = len(password)

if (cchUser and cchPass)
InternetSetOption(hResourceHandle, INTERNET_OPTION_USERNAME, user, cchUser+1)
InternetSetOption(hResourceHandle, INTERNET_OPTION_PASSWORD, password, cchPass+1)
endif

HttpSendRequestA(hResourceHandle, 0, 0, 0, 0)

UINT dwNumberOfBytesRead, dwNumberOfBytesWriten
while (InternetReadFile(hResourceHandle,buffer,1024,dwNumberOfBytesRead) and dwNumberOfBytesRead)

WriteFile(hTargetFile, buffer, dwNumberOfBytesRead, &dwNumberOfBytesWriten, 0)
size = size + dwNumberOfBytesWriten
endwhile

InternetCloseHandle(hResourceHandle)
endif
InternetCloseHandle(hConnectHandle)
endif
InternetCloseHandle(hOpenHandle)
endif
return size

philippe.tx

yessssssssssssssssssssss!
site$ = "http://virtual-loup-de-mer.org"
site$ = "virtual-loup-de-mer.org"
doc$="/ws/raceinfo/ranking.php?idr=20101231"
seems to work perfectly :)
many many thanks