download file with VBA from web

auto

Board Regular
Joined
May 20, 2012
Messages
53
hi, to all of you reading my post,
i know a lot of ppl already struggled with this what i go thru now, i wonder how ppl handled it,
i need to download a file from a website everyday in the morning, the webpage require a user and password, the file is a csv file, i got a code which will download a file from web but not when it require log-in, is there any way to add some coding which will log-in and do my work?
here is my code;

Sub Test()
Dim FileNum As Long
Dim FileData() As Byte
Dim MyFile As String
Dim WHTTP As Object

On Error Resume Next
Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5")
If Err.Number <> 0 Then
Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5.1")
End If
On Error GoTo 0

MyFile = "http://www.mywebsite.com/cvs_export.php"

WHTTP.Open "GET", MyFile, False
WHTTP.send
FileData = WHTTP.ResponseBody
Set WHTTP = Nothing

If Dir("C:\MyDownloads", vbDirectory) = Empty Then MkDir "C:\MyDownloads"

FileNum = FreeFile
Open "C:\MyDownloads\inventory.csv" For Binary Access Write As #FileNum
Put #FileNum, 1, FileData
Close #FileNum

MsgBox "Open the folder [ C:\MyDownloads ] for the downloaded file..."
End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
ok, i download fiddler, and i started a session, i went to raw, there is a raw tab on top of the program, and in the bottom, which one did i have to look on?
 
Upvote 0
here is what i have.
GET "here is the address of the website" HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Referer: "here is the address of the website"
Accept-Language: en-US
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
Accept-Encoding: gzip, deflate
Host: "here is the address of the website"
Connection: Keep-Alive
Cookie: osCsid=6c6084875e279291191eda0dcf666ddc
 
Last edited:
Upvote 0
it looks like the headers of the request when you load the website frontpage.

What I need is a raw view of a request from the session when you perform a login; and a raw view of a response to this request (in the window below).
And also, a raw view of request and response when you go to the page with the link to the file you need to download.

Edit out all sensitive information when you post.
 
Upvote 0
got you.
when i log in the raw view on top is.
POST mywebpage/login.php?action=process HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Referer:mywebpage/login.php
Accept-Language: en-US
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Host: mywebpage
Content-Length: 59
Connection: Keep-Alive
Pragma: no-cache
Cookie: osCsid=8c7adb0817709957b7d59647e98b633b

email_address=myusername&password=mypassword&x=24&y=7
 
Upvote 0
in the window below is this.
HTTP/1.0 302 Found
Date: Tue, 21 Aug 2012 23:46:54 GMT
Server: Apache/2.2.22 (Unix) mod_ssl/2.2.22 OpenSSL/0.9.8e-fips-rhel5 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635
X-Powered-By: PHP/4.4.9
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Location: mywebpage/index.php
Content-Length: 0
Connection: close
Content-Type: text/html
 
Upvote 0
and when i go to the download page this is what i got on top.
GET mywebpage/cvs_export.php HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Referer: mywebpage/account.php
Accept-Language: en-US
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
Accept-Encoding: gzip, deflate
Host: mywebpage
Connection: Keep-Alive
Cookie: osCsid=8c7adb0817709957b7d59647e98b633b
 
Upvote 0
and this is in the window below, for download.
HTTP/1.0 200 OK
Date: Tue, 21 Aug 2012 23:53:36 GMT
Server: Apache/2.2.22 (Unix) mod_ssl/2.2.22 OpenSSL/0.9.8e-fips-rhel5 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635
X-Powered-By: PHP/4.4.9
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Content-disposition: attachment; filename=EP2012Aug21-1604.csv
Pragma: no-cache
Expires: 0
Connection: close
Content-Type: text/x-csv
 
Upvote 0
in the window below is this.
HTTP/1.0 302 Found
Date: Tue, 21 Aug 2012 23:46:54 GMT
Server: Apache/2.2.22 (Unix) mod_ssl/2.2.22 OpenSSL/0.9.8e-fips-rhel5 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635
X-Powered-By: PHP/4.4.9
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Location: mywebpage/index.php
Content-Length: 0
Connection: close
Content-Type: text/html
here the server redirects you to /index.php. In Fiddler you should be able to see the request to this page and the response. Please post both.
 
Upvote 0

Forum statistics

Threads
1,216,030
Messages
6,128,407
Members
449,448
Latest member
Andrew Slatter

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top