csv - Vba dowload file from internet WinHttpReq with login not working -


i have been looking solution automatize dowload of csv table site, haven't found working solution.

if on ie or chrome, after previous log in enter url , file automatically start dowloading. @ purpose have way of achieving need through ie , html object navigating , saving, uses sendkeys , not suitable solution. tried achieve winhttpreq download me seems efficient , elegant way it. problem : it downloads file unfortunately outputs csv file html code of login page -> must fail logging in. site http.

following code found posted in several forums.

sub downloadfile()  dim myurl string valore = range("f6").value myurl = "www.myurlpointingtodownload.com"  dim winhttpreq object set winhttpreq = createobject("microsoft.xmlhttp") winhttpreq.open "get", myurl, false, "eimail@g.com", "passs" winhttpreq.send  myurl = winhttpreq.responsebody if winhttpreq.status = 200     set ostream = createobject("adodb.stream")     ostream.open     ostream.type = 1     ostream.write winhttpreq.responsebody     ostream.savetofile "c:\users\mee\desktop\fileoioi.csv", 2      ostream.close end if  end sub 

thank you, bob.

thanks alex , kyle's hints looked these 2 posts solved problem.

nb -> no cookie handling , fiddlr body copy of http post request 2 required steps me

here solution:

sub eds()  dim strcookie string, strresponse string, _   strurl string   dim xobj object   dim winhttpreq object   set xobj = new winhttp.winhttprequest    un = "2myusername"   pw = "mypass"    strurl = "http://my.website.com/"   xobj.open "post", strurl, false    xobj.setrequestheader "user-agent", "mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, gecko) chrome/33.0.1750.154 safari/537.36"   xobj.setrequestheader "content-type", "application/x-www-form-urlencoded"   xobj.send "username=" & un & "&password=" & pw & "&login=login"    strresponse = xobj.responsetext    strurl = http://my.website.com/date=09-10-1945%&search 'the url pointing csv file   xobj.open "get", strurl, false    xobj.setrequestheader "connection", "keep-alive"   xobj.setrequestheader "user-agent", "mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, gecko) chrome/33.0.1750.154 safari/537.36"   xobj.send    strcookie = xobj.getresponseheader("set-cookie")   strresponse = xobj.responsebody   if xobj.status = 200     set ostream = createobject("adodb.stream")     ostream.open     ostream.type = 1     ostream.write xobj.responsebody     ostream.savetofile "c:\users\me\desktop\ciao\file.csv", 1     ostream.close end if end sub 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -