PDA

View Full Version : send login details to web form using Microsoft.XMLHTTP



robert101083
06-10-2013, 01:11 AM
Hi,

I like to download a CSV file from a website. I can access this file using a direct link, but I need to login first before I can access this file.

What I have so far works just fine. I can open the url and save the content to a CSV file. But the thing being saved in the file at the moment is the HTML of the page which says I need to login first.

The VBA:

Public Sub ImportCSVData()
Dim BASE_URL As String
Dim URL As String
Dim TargetFile As String
Dim oStream

BASE_URL = "https://foo.com/"
URL = "https://foo.com/reports/1254.csv"
TargetFile = "./files/current_report_wip2.csv"

Dim WinHttpReq As Object
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")

WinHttpReq.Open "GET", URL, False
WinHttpReq.send

URL = WinHttpReq.ResponseBody
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1
oStream.Write WinHttpReq.ResponseBody
oStream.SaveToFile (TargetFile), 2
oStream.Close

End Sub

The login Form:

<form action="/user_session" class="form no_color" method="post" style="width:100%"><div style="margin:0;padding:0;display:inline"><input name="authenticity_token" value="QtLdYBJ+b0XIsuXe+Xqvue1MV5y4WmiuX9zLGf4frxM=" type="hidden"></div> <table style="margin-bottom:5px;font-weight:bold;width:100%">
<tbody><tr>
<td style="font-size:16px"><img alt="icon" src="/images/icon.png?1369048502" style="width:30px;height:30px;vertical-align:middle"> sign</td>
<td style="text-align:right;">Company Name</td>
</tr>
</tbody></table>
<div class="login_bgcolor">
<table cellpadding="5" cellspacing="7">
<tbody><tr>
<td><label class="inline_label_normal" for="user_session_email">Email</label></td>
<td><input class="inline_text_field" id="user_session_email" name="user_session[email]" size="30" type="text"></td>
</tr>
<tr>
<td><label class="inline_label_normal" for="user_session_password">Password</label></td>
<td><input class="inline_text_field" id="user_session_password" name="user_session[password]" size="30" type="password"></td>
</tr>
</tbody></table>

<div class="group1">
<button class="button" type="submit">
<img alt="Login" src="/images/web-app-theme/tick.png?1369048502"> Login
</button>
<div class="clear"></div>
</div>
<div>


</div>

<script src="/javascripts/jquery.notifyBar.js?1369048502" type="text/javascript"></script>
<style type="text/css">
.notice {
color: red;
margin-left: auto;
margin-right: auto;
text-align: center;
font-weight: bold;
}
</style>
<script>
var origWidth;
$(document).ready(function() {
origWidth = $(window).width(); //store the window's width when the document loads
$('.flash').delay(500).fadeIn('normal', function() {
$(this).delay(2500).fadeOut();
});
});

$(window).resize(function() {
var curWidth = $(window).width(); //store the window's current width
var delta = (curWidth- origWidth);
$("#login_box").offset({top:($("#login_box").offset().top + delta)});
origWidth = curWidth;

});
</script>



</div></form>

I do not wan't any browsers to open, it needs to run in the background.

I've tried a lot of different methods to login - for the last 4 hours, but they all didn't work so I leave them out. And cant remember which they all where either.

Thanks for any help!

Cheers,
Robert

Kenneth Hobs
06-10-2013, 06:05 AM
Try http://www.vbaexpress.com/forum/showthread.php?t=43015