Jump to content

Help for Cookies after login


 Share

Recommended Posts

Hi,

I developed a tool to automatically log into a web page:

#include "WinHttp.au3"
#Include <String.au3>
#include <Array.au3>

Opt("MustDeclareVars", 1)

Global $sFile = @DesktopDir & "\Skills.xml"
Global $sFile2 = @DesktopDir & "\Skills2.xml"
Global $hFile, $hfile2
Global $temp_file, $A_token, $P_token

global $p_csrfToken, $p_id



Global $hConnect, $sRead, $hrequest
Global $sContent = "Content-Type: application/x-www-form-urlencoded; charset=UTF-8"
global $sAddress = "mydomain.com"
global $spage = "login"
global $spage_2= "2ndpage?p_id="
Global $hOpen
global $sChunk, $sData


$hOpen = _WinHttpOpen()
$hConnect = _WinHttpConnect($hOpen, $sAddress)
_WinHttpSetOption($hOpen, $WINHTTP_OPTION_USER_AGENT, $sContent)
$sRead = _WinHttpSimpleFormFill($hConnect, $spage ,"logindata","name", "myname", "password", "mypw")

If @error Then
    msgbox(0,"","Error")
    Exit
Else
    $hFile = FileOpen($sFile, 2)
    FileWrite($hFile, $sRead)
    FileClose($hFile)
EndIf

Into this page i can find a unique ID I need to go further:

FileOpen($sfile)
$temp_file = FileRead($sfile)
$P_token = _StringBetween($temp_file,"p._id = '","';",-1)
$p_id = _ArrayToString($P_token)
fileclose($sfile)
$spage_2= $spage_2& $p_id

Now i need to go further, second page, using the url:

http://mydomain.com/2ndpage?p_id=$p_id (the one i got into previous lines)

I tried

$hRequest = _WinHttpOpenRequest($hConnect, _
        "GET", _ ; verb
        $spage_2=, _    ; target
        Default, _ ; version
        $sAddress, _    ; referer
        "*/*") ; accept

_WinHttpAddRequestHeaders($hRequest, "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3")
_WinHttpAddRequestHeaders($hRequest, "Accept-Language: it-it,it;q=0.8,en-us;q=0.5,en;q=0.3")
_WinHttpAddRequestHeaders($hRequest, "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
_WinHttpAddRequestHeaders($hRequest, "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7")
_WinHttpAddRequestHeaders($hRequest, "Keep-Alive: 115")
_WinHttpAddRequestHeaders($hRequest, "Connection: keep-alive")
_WinHttpAddRequestHeaders($hRequest, "Accept: application/json, text/javascript, */*")
_WinHttpAddRequestHeaders($hRequest, "Content-Type: application/x-www-form-urlencoded; charset=UTF-8")
_WinHttpAddRequestHeaders($hRequest, "Cache-Control: no-cache")

global $answer = _WinHttpSendRequest($hRequest)

;msgbox(0,"answer",$answer)

global $answer2 = _WinHttpReceiveResponse($hrequest)
If @error Then
    MsgBox(48, "Error", "Error ocurred for WinHttpReceiveResponse, Error number is " & @error)
Else

EndIf

global $data_avail = _WinHttpQueryDataAvailable($hRequest)
;msgbox(0,"data",$data_avail)

If _WinHttpQueryDataAvailable($hRequest) Then
    ; Read
    $hFile2 = FileOpen($sFile2, 2)
    FileWrite($hFile2, _WinHttpReadData($hRequest))
    FileClose($hFile2)
Else
    MsgBox(48, "Error", "Site is experiencing problems.")
EndIf

I always get no errors into msgboxes but always a 0 bytes file.

I think problem is due to cookies, since it seems he can't remember previous credentials.

How can it be solved?

Thanks in advance,

M.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...