Jump to content

WinHttp redirection


Recommended Posts

Hi everyone,

I already did a website automation that sends requests for my company using IE UDF (its a very simple code - Login and then every few minutes refresh the website and retrieves data) . The company that transmits requests uses a Google acc validation and so I have to login with my "Google account". I'm not very experienced and after reading and researching about the WinHttp I thought I could accomplish the same task automation using this method. Here is what i'm trying to do and what do i got so far:

 

  • Login using my google acc (as an example I will use youtube to redirect because the partner website is restricted). Here i need to imput my Google account and password. Its the same for the Gmail access. I was able to login, but the website dont redirect to the URL after the login.
Spoiler
#include "WinHttp.au3"

Global $sRead, $hOpen, $hConnect
Global $sUsername, $sPassword
$sUsername = "Email"
$sPassword = "Password"
Global $sUrl = "https://accounts.google.com/ServiceLogin?hl=pt-BR&uilel=3&service=youtube&passive=true&continue=https%3A%2F%2Fwww.youtube.com%2Fsignin%3Ffeature%3Dsign_in_button%26next%3D%252F%26hl%3Dpt%26action_handle_signin%3Dtrue%26app%3Ddesktop#identifier"

$hOpen = _WinHttpOpen('Mozilla/5.0 (Windows NT 5.1; rv:2.0) Gecko/20100101 Firefox/4.0'); create new session
$hConnect = _WinHttpConnect($hOpen, $sUrl)

$sRead = _WinHttpSimpleFormFill($hConnect, _
  "", "gaia_loginform", _
  "name:Email", $sUsername, _
  "type:submit", 0)

$hConnNew = $sRead
$sHTM = _WinHttpSimpleFormFill($hConnNew, _
  $hOpen, "gaia_loginform", _
  "name:Passwd", $sPassword, _
  "type:submit", 0)

If @error Then
   MsgBox(0, "Error", "Error: " & @error)
Else
    ConsoleWrite(@CRLF & $sHTM & @CRLF & @CRLF)
EndIf

_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)
Exit

 

  1. I can login but i dont get redirected to the URL (in this example for the youtube). How can i achieve this redirection?
  2. How can i keep the session alive and refresh the URL every a few minutes to check if we got any new requests. (The logical part of the Loop i belive can do  with TimerInit and TimerDiff).

I will be very grateful for any help. :) Thanks in advance.

Link to comment
Share on other sites

Hi BetaLeaf. Thanks for the input. :)

I can make the loop part using your suggestion. But im still struggling to get the "auto redirection" that happens when you paste that URL into a browser and login, or even make another _WinHttpConnect() to the desired website (but this needs to be in the same session because the website requires you to be logged).

Thanks again.

Link to comment
Share on other sites

Where do you actually use the action page?

Try it like this:

#include "WinHttp.au3"

Global $sRead, $hOpen, $hConnect
Global $sUsername, $sPassword
$sUsername = "Email"
$sPassword = "Password"
Global $sUrl = "https://accounts.google.com/"
$sActionPage = "ServiceLogin?hl=pt-BR&uilel=3&service=youtube&passive=true&continue=https%3A%2F%2Fwww.youtube.com%2Fsignin%3Ffeature%3Dsign_in_button%26next%3D%252F%26hl%3Dpt%26action_handle_signin%3Dtrue%26app%3Ddesktop#identifier"

$hOpen = _WinHttpOpen('Mozilla/5.0 (Windows NT 5.1; rv:2.0) Gecko/20100101 Firefox/4.0'); create new session
$hConnect = _WinHttpConnect($hOpen, $sUrl)

$sRead = _WinHttpSimpleFormFill($hConnect, _
  $sActionPage, "gaia_loginform", _
  "name:Email", $sUsername, _
  "type:submit", 0)

$hConnNew = $sRead
$sHTM = _WinHttpSimpleFormFill($hConnNew, _
  $hOpen, "gaia_loginform", _
  "name:Passwd", $sPassword, _
  "type:submit", 0)

If @error Then
   MsgBox(0, "Error", "Error: " & @error)
Else
    ConsoleWrite(@CRLF & $sHTM & @CRLF & @CRLF)
EndIf

_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)
Exit

Do you see the difference?
Btw, why that user agent string?

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

Hi trancexx. First many thanks for the UDF and for the help :). I imagine that you have spent alot of time building it. Answering your questions:

  1. "Do you see the difference?" I do now, i tryed something similar but in the second formfill i was using the action page when i should use the handler of the open session. Probably a nb mistake but everything is new to me.
  2. "Btw, why that user agent string?" I was getting sometimes an error saying that the browser is old and outdated and etc so i was trying to use a agent string to test.

You example works great, actually there was another website were i need to do some form fills (i was using the IE UDF), and i did it, its working great and is faster than using IE. (your answer in another thread about multiple forms fills helped me alot - thanks again :)).

I tried to replicate the above example in another website and i keep getting error 4. (the redirect website is only accessible to a few members).

Spoiler
#include "WinHttp.au3"

Global $sRead, $hOpen, $hConnect, $hConnNew, $sActionPage, $sUsername, $sPassword, $sUrl

$sUsername = "Email"
$sPassword = "Password"
$sUrl = "accounts.google.com"
$sActionPage = "ServiceLogin?service=ah&passive=true&continue=https%3A%2F%2Fappengine.google.com%2F_ah%2Fconflogin%3Fcontinue%3Dhttps%3A%2F%2Fwww.raterhub.com%2F&ltmpl=gm&shdf=ChULEgZhaG5hbWUaCVJhdGVyIEh1YgwSAmFoIhThoWkIjshJkJPbt1Ui-tTuWDmknygBMhSVMFKrRs3jzf4xp8WmlV9ZQ5N-cw#identifier"

$hOpen = _WinHttpOpen()
$hConnect = _WinHttpConnect($hOpen, $sUrl)

$sRead = _WinHttpSimpleFormFill($hConnect, _
  $sActionPage, "gaia_loginform", _
  "name:Email", $sUsername, _
  "type:submit", 0)

$hConnNew = $sRead

$sHTM = _WinHttpSimpleFormFill($hConnNew, _
  $hOpen, "gaia_loginform", _
  "name:Passwd", $sPassword, _
  "type:submit", 0)

If @error Then
   MsgBox(0, "Error", "Error: " & @error)
Else
    ConsoleWrite(@CRLF & $sHTM & @CRLF & @CRLF)
EndIf

_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)
Exit

 

May i bother you with one more question? How can i keep the session alive (so i wont need to login again) and refresh the current landing page every a few minutes. There is an example you can point me? Thanks again for your time.

Link to comment
Share on other sites

Try adding "https://" before "accounts.google.com" to force secure connection. Maybe it's security issue.

You don't need session alive. Just repeat everything. Cookies aren't persistent with WinHttp and there shouldn't be problems. If you insist, just don't close $hOpen and reuse it later (until server kills it).

One more thing... You aren't ever closing $hConnNew. That's memory leak.

 

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

Oh and welcome to the forums, @NatalyB. On behalf of the community, we hope you enjoy your stay and find it educational. Cheers.

 

Jeff Savage ~ BetaLeaf

 

P.S. @trancexx Congrats on exactly 6,666 posts. I was going to pm ya, but you cannot accept private messages for whatever reason.

Edited by BetaLeaf

 

 

Link to comment
Share on other sites

@trancexx Thanks for the input again. :) Tried to put the "https://" before and i'm still getting error 4, dont know why. Is my aproach wrong? I also tried another thing, to login the main google acc website and then go to the website and check if it would work, but still gives me error. To confirm if i could do that i tried first with the youtube, but when i check after connecting to the youtube url i saw that i was not logged. The code i used:

#include "WinHttp.au3"

Global $sRead, $hOpen, $hConnect, $hConnNew, $sActionPage, $sUsername, $sPassword, $sUrl
Global $hConnect2, $hRequest2, $sChunk, $sData

$sUsername = "Email"
$sPassword = "Password"
$sUrl = "https://accounts.google.com"
;$sActionPage = "ServiceLogin?service=ah&passive=true&continue=https%3A%2F%2Fappengine.google.com%2F_ah%2Fconflogin%3Fcontinue%3Dhttps%3A%2F%2Fwww.raterhub.com%2F&ltmpl=gm&shdf=ChULEgZhaG5hbWUaCVJhdGVyIEh1YgwSAmFoIhThoWkIjshJkJPbt1Ui-tTuWDmknygBMhSVMFKrRs3jzf4xp8WmlV9ZQ5N-cw#identifier"
$sActionPage = "ServiceLogin?passive=1209600&continue=https%3A%2F%2Faccounts.google.com%2FManageAccount&followup=https%3A%2F%2Faccounts.google.com%2FManageAccount#identifier"

$hOpen = _WinHttpOpen()
$hConnect = _WinHttpConnect($hOpen, $sUrl)

$sRead = _WinHttpSimpleFormFill($hConnect, _
  $sActionPage, "gaia_loginform", _
  "name:Email", $sUsername, _
  "type:submit", 0)

$hConnNew = $sRead

$sHTM = _WinHttpSimpleFormFill($hConnNew, _
  $hOpen, "gaia_loginform", _
  "name:Passwd", $sPassword, _
  "type:submit", 0)

If @error Then
   MsgBox(0, "Error", "Error: " & @error)
Else
    ConsoleWrite(@CRLF & $sHTM & @CRLF & @CRLF)
EndIf

$hConnect2 = _WinHttpConnect($hOpen, "youtube.com")
; Specify the reguest
$hRequest2 = _WinHttpOpenRequest($hConnect2)
; Send request
_WinHttpSendRequest($hRequest2)
; Wait for the response
_WinHttpReceiveResponse($hRequest2)

; Check there is data available...
If _WinHttpQueryDataAvailable($hRequest2) Then
    ; Read
    While 1
        $sChunk = _WinHttpReadData($hRequest2)
        If @error Then ExitLoop
        $sData &= $sChunk
    WEnd
    ConsoleWrite("###########################################################################")
    ConsoleWrite(@CRLF & @CRLF & @CRLF & $sData & @CRLF) ; print to console
Else
    MsgBox(48, "Error", "Site is experiencing problems.")
EndIf

_WinHttpCloseHandle($hConnect2)
_WinHttpCloseHandle($hRequest2)

_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hConnNew)
_WinHttpCloseHandle($hOpen)
Exit

@BetaLeaf Thanks. :) I'm learning a lot. I must confess, for a person who knows nothing of programming it is a challenge. But i will get there.

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...