PDA

View Full Version : download via Microsoft.XMLHTTP fails



med
03-24-2016, 08:06 AM
Hi,
i'm trying to download *.csv and xls file from a website into my C drive.I'm using the code below .



url="myurl/levelt1/level11/mycsvfile.csv"

Set HttpQuery = CreateObject("Microsoft.XMLHTTP")
HttpQuery.Open "GET", url, False, myusername, myPasswd
HttpQuery.send

url = HttpQuery.responseBody

If HttpQuery.Status = 200 Then
Set wstream = CreateObject("ADODB.Stream")
wstream.Type = 1
wstream.Open
wstream.Write HttpQuery.responseBody
wstream.SaveToFile "c:\test\myfile.csv", 2
wstream.Close
End If


The Problem: this code create the file on my C drive , but its content is not correct , the file contains the html code of the authentication page of the web site.

the credentials i'm using are corrects , in addition when i launch my URL in my Internet explorer the download is done without problems.
I'm really stuck on this , any help please ?

thanks in advance.
Med.

mancubus
03-24-2016, 08:51 AM
welcome to vbax.

see if this helps:

http://www.vbaexpress.com/forum/showthread.php?33145-Download-from-Excel-via-URLDownloadToFile

med
03-24-2016, 09:11 AM
Thanks for that related topic.
I've tried the method described in that post and i've exactly the same result : a csv file with html content of the login page... :dunno

mancubus
03-24-2016, 02:59 PM
if you need authentication perhaps below will give you some pointers...

http://stackoverflow.com/questions/22675768/vba-to-download-csv-file-from-a-website-with-login-and-password

med
03-25-2016, 06:42 AM
no , it does not do it.
Actually the login URL of my site is not structured to see clearly the username and pwd ,to add the credantials as input . i can't use this so.

mancubus
03-25-2016, 07:01 AM
?
http://stackoverflow.com/questions/22863408/vba-dowload-file-from-internet-winhttpreq-with-login-not-working


or do a search on Google with keywords like "dowload csv file from internet with login" and try recommended methods, if any...

med
03-29-2016, 01:21 AM
according to the htm code i see in my downloaded files, i think it's about cookies , the html displayed look like i'm connecting to the website for the first time and it does detect any previous connection .
i've tried to use the SetRequestHeader method of the HttpQuery object but in vain.
Anyone knows how i can specify to this SetRequestHeaderto use THE cookies of my website ? thx

snb
03-29-2016, 02:25 AM
Can you apporach this website as FTP-site ?