Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Excel to log in using XMLHTTP

Status
Not open for further replies.

zunni

Technical User
Oct 30, 2006
9
GB
Hi,

I am experimenting with using XMLHTTP and the POST method in VBA to log into a local webserver, but I can't seem to send the right parameters to submit the login details. I have extracted the HTML code for the form element below:

<form name="frmLogin" method="Post" action="login_2.asp?Submit=YY&iatt=">
<input type="text" name="txtUserName">
<input type="password" name="txtUserPass">
<input type="submit" name="LoginSubmit" value="Login" style="Cursor: Hand">
</form>

and the VBA code I am using to logo is below:

Set XMLHttpRequest = New MSXML2.xmlhttp

With XMLHttpRequest
.Open "POST", "login_2.asp?Submit=YY&iatt=", False
.setRequestHeader "Content-Type", "application/x- .send "txUserName=a+username&txtUserPass=somenumber"
End With

When I run the macro, the webserver says that I have not submitted any information.

Can anyone help?
 
Have you read this ?
faq707-6399

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi,

I havent read that post before but I am very confident in using the InternetExplorer object to navigate and complete forms in VBA and can log in using this method (and have done so already). However, I want to experiment with XMLHTTP and want to see how the two methods differ in terms of versatility but I can't seem to communicate with the webserver properly.
 
zunni,
This works for me with MS XML 2.6 in Win XP using IE7.

I don't normally use IE so the "Remember Me" cookie is not set, after running this I navigated to Tek-Tips in IE and it remembered my username.

Code:
  Dim XMLHttpRequest As New MSXML2.xmlhttp
    
  With XMLHttpRequest
   .Open "POST", "[URL unfurl="true"]http://www.tek-tips.com/passcheck.cfm",[/URL] False
   .setRequestHeader "Content-Type", "application/x-[URL unfurl="true"]www-form-urlencoded"[/URL]
   .send "method=login&handle=cautionmp&pass=*"
  End With

  Debug.Print XMLHttpRequest.responseText  
  Stop
  
  Set XMLHttpRequest = Nothing

My snipit is almost the same as your except:
[ol][li]In the [tt].Open[/tt] statement I used the full URL.[/li]
[li]In the [tt].Open[/tt] statement I didn't include any of the query string.[/li]
[li]Included the complete query string in the [tt].Send[/tt] method.[/li][/ol]

Hope this helps,
CMP

[small]For the best results do what I'm thinking, not what I'm saying.[/small]
(GMT-07:00) Mountain Time (US & Canada)
 
Hi

Thanks for your reply. In response to your points:

1. In my .open statement I am using the full URL, just have not included it in my example.

2 and 3. I have tried it with and without some of the query string. From the HTML code:

<form name="frmLogin" method="Post" action="login_2.asp?Submit=YY&iatt=">

There is some of the query string included in the action parameter. Why is it there?

Also, from viewing the page source of login_2.asp page, I notice there is no asp code nor a reference to an include file? Does this mean that the code that I am looking at is not the asp page?

Stuck.
 
Does this mean that the code that I am looking at is not the asp page?
The most likely is that you're looking at the CLIENT SIDE stuff ...
 
zunni,
Don't know if the snipit you posted was the actual code you were using but your missing the second [tt]t[/tt] in [tt]tx[red]t[/red]UserName[/tt]
Code:
...
[tab].send "txUserName=a+username&txtUserPass=somenumber"
...

If that still doesn't work try adding[tt]&LoginSubmit=Login[/tt] and see if that works.

CMP
 
Arghh!

Such a simple error
 
Oh oh!

I have hit another snag

While the .responsetext shows that I have successfully logged in and shows the html of the next webpage. I can't navigate to it using IE like in the tek-tips example above. Does anyone know why?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top