VBA WinHTTPRequest and submitting forms
Posted
by Hazerider
on Stack Overflow
See other posts from Stack Overflow
or by Hazerider
Published on 2010-04-16T13:05:02Z
Indexed on
2010/04/16
13:23 UTC
Read the original article
Hit count: 1873
Hi.
I spent all day yesterday trying to figure out how to submit a form using WinHTTPRequest
. I can do it pretty easily with an InternetExplorer
object, but the problem is that I need to save a PDF file that gets returned, and I am not sure how to do this with the IE object. Here is the relevant HTML code snippet:
<div class="loginHome-left">
<fieldset>
<h3>Log in Using</h3>
<form>
<label for="standardLogin" accesskey="s">
<input name="useLogin" id="standardLogin" value="standard"
type="radio" checked="true">Standard Login</label>
<label for="rsaSecurID" accesskey="r">
<input name="useLogin" value="rsaSecur" type="radio" id="rsaSecurID"
onclick="redirectLogin('ct_logon_securid');return false;">RSA SecurID</label>
<label for="employeeNTXP" accesskey="e">
<input name="useLogin" id="employeeNTXP" value="employee"
type="radio" onclick="redirectLogin('ct_logon_external_nt');return false; ">
Employee Windows Login<br></label>
</form>
<br>
<div class="error">Error: ...</div><br>
<form onSubmit="if(validate(this)) {formSubmit();} return false;"
name="passwdForm" method="post" action="/UAB/ct_logon">
<input value="custom" name="pageId" type="hidden">
<input value="custom" name="auth_mode" type="hidden">
<input value="/UAB/ct_logon" name="ct_orig_uri" type="hidden">
<INPUT VALUE="" NAME="orig_url" TYPE="hidden">
<input value="" name="lpSp" type="hidden">
<label for="user">
<strong>Username</strong>
</label>
<input autocomplete="off" name="user" type="text"
value="" class="txtFld" onkeypress="return handleEnter(this, event);">
<br>
<label for="EnterPassword">
<strong>Password</strong> (<a tabindex="-1" href="/UAB/BCResetWithSecrets">Forgot Your Password?</a>)
</label>
<input autocomplete="off" name="password" type="password"
class="txtFld" onkeypress="return handleEnter(this, event);">
<INPUT id="rememberLogin" name="lpCookie" type="checkbox">
<label for="rememberLogin">Remember My Login Information</label><br>
</form>
<div class="right">
<br>
<input type="image" src="/BC_S/images/bclogin/btn_login.gif"
name="" value="Submit"
onClick="if(validate(document.forms['passwdForm'])){formSubmit();}return false;">
</div>
<div class="clearfix"></div>
</fieldset>
</div>
In order to log in through InternetExplorer
, I do the following:
Sub TestLogin()
Dim ie As InternetExplorer, doc As HTMLDocument, form As HTMLFormElement, inp As Variant
Set ie = New InternetExplorer
ie.Visible = True
ie.navigate "https://URL of the login page"
Do Until ie.readyState = READYSTATE_COMPLETE
Loop
Set doc = ie.document
For Each form In doc.forms
If InStr(form.innerText, "Password") <> 0 Then
form.elements("user").Value = "my_name"
form.elements("password").Value = "my_password"
Exit For
Else
End If
Next
'This is the unnamed input with an image that is used to submit the form'
doc.all(78).Click
ie.navigate "https://url of the PDF"
Do Until ie.readyState = READYSTATE_COMPLETE
Loop
Dim filename As String, filenum As Integer
filename = "somefile.pdf"
filenum = FreeFile
Open filename For Binary Access Write As #filenum
Write #filenum, doc.DocumentElement.innerText
Close #filenum
ie.Quit
Debug.Print
Set ie = Nothing
End Sub
What I really would like to do is something along the lines of the following:
Sub TestLogin3()
Dim whr As New WinHttpRequest, postData As String
whr.Open "POST", "https://live.barcap.com/UAB/ct_logon", False
whr.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
whr.setRequestHeader "Connection", "Keep-Alive"
whr.Send
whr.WaitForResponse
postData = "user=paschom1&password=change01"
'Or the following?'
postData = "user=paschom1&password=change01&orig_url=&pageId=custom&auth_mode=custom&ct_orig_uri=/BC/dispatcher&lpSp=&lpCookie=off"
whr.Send postData
whr.WaitForResponse
Debug.Print whr.responseText
End Sub
It just refuses to work though. Not sure if I need to use more setRequestHeader
with Content-Form
or something similar, and if I do, not sure what exactly I am supposed to pass it. If anyone has any advice regarding this, it would be hugely appreciated. I could probably use a perl module to do it, but I would rather keep it all in VBA if possible.
Thanks, Marc.
© Stack Overflow or respective owner