Sign in to follow this  
Followers 0
Hadez

HTTp POST Redirect after login

20 posts in this topic

Im wondering if anyone can help me with a http post problem. Iam able to log into a site easily enough but i need to be able to get to another page on the site after that, but im unsure how. currently im using the code below to login but i need to get to the http://POSTWebsite.com/forum.php page after login so that i can post a new thread. anyone got any ideas how to do this?

$oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
$oHTTP.Open("POST", "http://POSTWebsite.com/login.php" , False)
$oHTTP.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10 (.NET CLR 4.0.20506)")
$oHTTP.SetRequestHeader("Referrer", "http://www.google.com")
$oHTTP.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
$oHTTP.SetRequestHeader("Connection", "keep-alive")
$oHTTP.Send("username=xxxxxxx" & "&password=xxxxxxx")
$oReceived = $oHTTP.ResponseText

Thanks for any help :graduated:

Share this post


Link to post
Share on other sites



Well if you are logged-in then just go to that page, your session should have cookies aboard.

Try WinHttp.au3, it comes with many advanced features of WinHTTP that you can access in no time. Search the forums.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

ive had a look at WinHttp.au3 but didnt understand it. When you said i should just go to that page, ive tried adding

$oHTTP.Open("POST", "http://POSTWebsite.com/forum.php" , False)

at the end of the script but $oReceived = $oHTTP.ResponseText shows that that page doesnt load. Ive read through every forum post in every forum thread but wasnt able to scrape the info from it. Any help with this? Thanks :graduated:

Share this post


Link to post
Share on other sites

ive had a look at WinHttp.au3 but didnt understand it.

That's too bad.

You should have looked harder. You even have a help file available with plenty of examples.

When you said i should just go to that page, ive tried adding

$oHTTP.Open("POST", "http://POSTWebsite.com/forum.php" , False)

at the end of the script but $oReceived = $oHTTP.ResponseText shows that that page doesnt load. Ive read through every forum post in every forum thread but wasnt able to scrape the info from it. Any help with this? Thanks :graduated:

Do you normally access that page with POST verb?

♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

I relooked at WinHttp.au3. there are good examples, but the problem is that i cant find anywhere that says what the parameters are. E.G

$sRead = _WinHttpSimpleFormFill($hConnect, "html/html_forms.asp", "name:input0", "name:user", "OMG!!!")

i see that but dont know what the parameters are. i have no idea what is supposed to go between each comma, so cant figure out how to modify the examples to do what i want. thats why i asked for help here.

Edited by Hadez

Share this post


Link to post
Share on other sites

Regarding WinHttp.au3...

That's why the help file was written.

_WinHttpSimpleFormFill. That page is not descriptive enough for you? Every parameter for the function is described, function defined, additional explanation on the function behavior, link to dependencies and four examples of usage. If that's not enough for you then maybe you aren't trying hard enough.

Would you like me to write the code you need for you?


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

as good as the examples are, i still cant work out how to redirect to another page after logging in. It must be that im not trying hard enough then :graduated: Either that, or there is something wrong with the site im trying to POST to :(

Edited by Hadez

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

now i defo need help. i checked with the site im using POST with and can confirm its my code thats the problem. i can login, but cant redirect to a page on that site and post data there, im using the code below. If you could point out where i going wrong, i would appreciate it as ive been stuck here for ages.

#include "WinHttp.au3"

Opt("MustDeclareVars", 1)
; Initialize and get session handle
Global $hOpen = _WinHttpOpen()

Global $hConnect, $sRead, $hConnect2, $sRead2
Global $hFileHTM, $sFileHTM = @ScriptDir & "\Form.htm"

; Get connection handle
$hConnect = _WinHttpConnect($hOpen, "website.com")

$sRead = _WinHttpSimpleFormFill($hConnect, "takelogin.php", "takelogin.php", "name:username", "xxxxxxx", "name:password", "xxxxxxx")

$hConnect2 = _WinHttpConnect($hOpen, "website.com")

$sRead2 = _WinHttpSimpleFormFill($hConnect2, "index.php", "shoutbox_form", "name:shoutbox_message", "test", "name:shout_submit", "Shout")
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hConnect2)

If $sRead2 Then
    MsgBox(64 + 262144, "Done!", "Will open returned page in your default browser now." & @CRLF & _
            "It should show sent data.")
    $hFileHTM = FileOpen($sFileHTM, 2)
    FileWrite($hFileHTM, $sRead2)
    FileClose($hFileHTM)
    ShellExecuteWait($sFileHTM)
EndIf

_WinHttpCloseHandle($hOpen)
Edited by Hadez

Share this post


Link to post
Share on other sites

no1 knows the problem? ive tried so many variations to the examples to try to get a redirect. when i do successfully get a redirect, its to the wrong page lol. I just need to know if its the format thats wrong so i can cancel the issue down to incorrectly using the parameters. Thanks to anyone who can shed some light on the problem.

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Try this code.

I'm sure it will help.

If StringInStr($oReceived, "Login message you get when you log in here..") Then 
; ...<actions>

Edit: Oh when you redirect to a page be sure to add it to your $oHTTP.Send()

Ex: $oHTTP.Send("username=xxxxxxx" & "&password=xxxxxxx" & "text that redirects you to a certain page here...")

Edited by Kalin

Share this post


Link to post
Share on other sites

That first form that you fill is way off. Did you copy/paste those "name:username" and "name:password" strings???

If yes then there is no sense in helping you. You wouldn't understand.

This has nothing to do with coding. You have to know what to code before actually attempting to code.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

What trancexx said...

You have to know exactly what you're sending before attempting to request interface with HTTP.

Otherwise you'll just be sending invalid data.

You should find some good HTTP Headers.

Share this post


Link to post
Share on other sites

I use the FireFox addon "Live HTTP Headers" to capture the headers send and received by FireFox for the action I want to automate, this gives you an idea on the headers to write yourself.

Share this post


Link to post
Share on other sites

The name:username etc are the field names. i used debugbar to get every fieldname. The first page is way off? That first page was a login page and the response showed that i logged in ok. I filled it in according to _WinHttpSimpleFormFill. Oh, and i already wrote a fully working script to do what i want using IE, but wanted to use POST instead as it is alot more convenient than having to open IE. Im pretty sure i what what im coding, just struggling with how to do it. I'll now look into HTTP headers. See if that is where my issue is. Thanks :graduated:

Share this post


Link to post
Share on other sites

Thanks for the info on headers. That seemed to be the problem. I decided to use $oHTTP = ObjCreate("winhttp.winhttprequest.5.1") instead of WinHttp.au3. I have now successfully redirected to another page. i have a question though. It seems some of the fields on that page have different content types to others. If im sending data to 1 page, do i use the pages Content-Type: multipart/form-data and then set a content type for each field that requires it or just use the pages content-type? E.G.

The pages content type is:

Content-Type: multipart/form-data

there are some fields on that page that i need to send data to like:

Content-Disposition: form-data; name="file"; filename="C:\Testfolder\test.txt"
Content-Type: application/octet-stream

2 fields have this content type but the others dont. Is this important or can i use the pages Content-Type?

$oHTTP.SetRequestHeader("Content-Type", "multipart/form-data")
$oHTTP.Send()

Share this post


Link to post
Share on other sites

share the working code please


muppet hands are so soft :)

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

#include <File.au3>
#include "WinHttp.au3"

Opt("MustDeclareVars", 1)
; Initialize and get session handle
Global $hFileHTM, $sFileHTM = @ScriptDir & "\Form.htm"
Global $oHTTP, $oReceived,

$oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
;Post request
$oHTTP.Open("POST", "http://website.com/takelogin.php" , False) 
;Add User-Agent header
$oHTTP.SetRequestHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR ")
;Add Referrer header$oHTTP.SetRequestHeader("Referrer", "http://www.REFERRERURL.com")
$oHTTP.SetRequestHeader("Referrer", "http://website.com/login.php")
;Add Content-Type
$oHTTP.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
$oHTTP.SetRequestHeader("Connection", "keep-alive")
;$oHTTP.Send()
$oHTTP.Send("username=xxxxxx" & "&password=xxxxxx" & "&index.php?logged=true")
;now open next page
$oHTTP.Open("POST", "http://website.com/upload.php" , False)
;Add User-Agent header
$oHTTP.SetRequestHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR ")
;Add Referrer header$oHTTP.SetRequestHeader("Referrer", "http://www.REFERRERURL.com")
$oHTTP.SetRequestHeader("Referrer", "http://website.com/upload.php")
;Add Content-Type
$oHTTP.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
$oHTTP.SetRequestHeader("Connection", "keep-alive")
$oHTTP.Send("subject=title.of.the.file" & "&message=this+is+a+description")
$oReceived = $oHTTP.ResponseText

If $oReceived Then
    MsgBox(64 + 262144, "Done!", "Will open returned page in your default browser now." & @CRLF & _
            "It should show sent data.")
    $hFileHTM = FileOpen($sFileHTM, 2)
    FileWrite($hFileHTM, $oReceived)
    FileClose($hFileHTM)
    ShellExecuteWait($sFileHTM)
EndIf

This successfully redirected to another page and posted to that page. I was unable to go further and figure out how to upload a file so went with curl instead. Hope it helps you.

Edited by Hadez

Share this post


Link to post
Share on other sites

#include <File.au3>
#include "WinHttp.au3"

Opt("MustDeclareVars", 1)
; Initialize and get session handle
Global $hFileHTM, $sFileHTM = @ScriptDir & "\Form.htm"
Global $oHTTP, $oReceived,

$oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
;Post request
$oHTTP.Open("POST", "http://website.com/takelogin.php" , False) 
;Add User-Agent header
$oHTTP.SetRequestHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR ")
;Add Referrer header$oHTTP.SetRequestHeader("Referrer", "http://www.REFERRERURL.com")
$oHTTP.SetRequestHeader("Referrer", "http://website.com/login.php")
;Add Content-Type
$oHTTP.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
$oHTTP.SetRequestHeader("Connection", "keep-alive")
;$oHTTP.Send()
$oHTTP.Send("username=xxxxxx" & "&password=xxxxxx" & "&index.php?logged=true")
;now open next page
$oHTTP.Open("POST", "http://website.com/upload.php" , False)
;Add User-Agent header
$oHTTP.SetRequestHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR ")
;Add Referrer header$oHTTP.SetRequestHeader("Referrer", "http://www.REFERRERURL.com")
$oHTTP.SetRequestHeader("Referrer", "http://website.com/upload.php")
;Add Content-Type
$oHTTP.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
$oHTTP.SetRequestHeader("Connection", "keep-alive")
$oHTTP.Send("subject=title.of.the.file" & "&message=this+is+a+description")
$oReceived = $oHTTP.ResponseText

If $oReceived Then
    MsgBox(64 + 262144, "Done!", "Will open returned page in your default browser now." & @CRLF & _
            "It should show sent data.")
    $hFileHTM = FileOpen($sFileHTM, 2)
    FileWrite($hFileHTM, $oReceived)
    FileClose($hFileHTM)
    ShellExecuteWait($sFileHTM)
EndIf

This successfully redirected to another page and posted to that page. I was unable to go further and figure out how to upload a file so went with curl instead. Hope it helps you.

I am using peices of this to log into my forums and retrive a vaule on the page. I got it working with what you posted first but its like it doesnt really log the user in becuase i can use the same user and pass in IE and it wont bump the script off :graduated:

muppet hands are so soft :)

Share this post


Link to post
Share on other sites

what is file.au3?


muppet hands are so soft :)

Share this post


Link to post
Share on other sites

It's one of the many UDF libraries included in AutoIt.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0