Bueno he estado modificando el ejemplo de _WinHttpAddRequestHeaders de winhttp.au3 para hacer el login del foro AutoIt
pero muestra "¡No hay datos disponibles."
Esto es lo que he intentado hasta ahora:
Código: Seleccionar todo
#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#include "WinHttp.au3"
Opt("MustDeclareVars", 1)
; !!!Note that this example will fail because of invalid username and password!!!
Global $sAddress = "http://www.autoitscript.com"; Tambien intenté http://www.autoitscript.com/forum/index.php?app=core&module=global§ion=login
Global $sUserName = "myusername"
Global $sPassword = "mypassword"
; Post data:
Global $sPostData =
"referer=http%3A%2F%2Fwww.autoitscript.com%2Fforum%2Findex.php%3Fact%3Didx&username="&$sUserName&"&password="&$sPassword
&"&rememberMe=1"
; Initialize and get session handle
Global $hOpen = _WinHttpOpen("Mozilla/5.0 (Windows; U; Windows NT 5.1; es-ES; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10
(.NET CLR 3.5.30729)")
; Get connection handle
Global $hConnect = _WinHttpConnect($hOpen, $sAddress)
; Make a request
Global $hRequest = _WinHttpOpenRequest($hConnect, _
"POST", _
"/forum/index.php?app=core&module=global§ion=login&do=process", _
Default, _
"http://www.autoitscript.com/forum/index.php?app=core&module=global§ion=login", _
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
; Enable cookies for sure
_WinHttpSetOption($hRequest, $WINHTTP_OPTION_DISABLE_FEATURE, $WINHTTP_DISABLE_COOKIES)
; Add header fields to the request
_WinHttpAddRequestHeaders($hRequest, "Accept-Language: es-es,es;q=0.8,en-us;q=0.5,en;q=0.3")
_WinHttpAddRequestHeaders($hRequest, "Content-Type: application/x-www-form-urlencoded")
_WinHttpAddRequestHeaders($hRequest,"Accept-Encoding: gzip,deflate")
_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")
; Send it
_WinHttpSendRequest($hRequest, $WINHTTP_NO_ADDITIONAL_HEADERS, $sPostData)
; Wait for the response
_WinHttpReceiveResponse($hRequest)
; Check if there is a response
Global $sHeader, $sReturned
If _WinHttpQueryDataAvailable($hRequest) Then
$sHeader = _WinHttpQueryHeaders($hRequest)
MsgBox(64, "Header", $sHeader)
Do
$sReturned &= _WinHttpReadData($hRequest)
Until @error
; Print returned
ConsoleWrite($sReturned)
Else
ConsoleWriteError("!No data available." & @CRLF)
MsgBox(48, "Failure", "No data available.")
EndIf
; Close handles
_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)
Utilizé el plugin live http headers de Firefox para ver lo que estaba siendo enviado mientras me logueaba manualmente (Edité mis datos y
la cookie por si acaso):
Código: Seleccionar todo
http://www.autoitscript.com/forum/index.php?app=core&module=global§ion=login&do=process
POST /forum/index.php?app=core&module=global§ion=login&do=process HTTP/1.1
Host: www.autoitscript.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; es-ES; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10 (.NET CLR
3.5.30729)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: es-es,es;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Referer: http://www.autoitscript.com/forum/index.php?app=core&module=global§ion=login
Cookie: autoitforumsession_id=***; autoitforumitemMarking_forums_items=***; autoitforummember_id=***;
autoitforumpass_hash=***; autoitforumcoppa=***; autoitforummqtids=***; autoitforumsfc=***; autoitforumsfct=WinHttp.au3;
autoitforumanonlogin=-1
Content-Type: application/x-www-form-urlencoded
Content-Length: 125
referer=http%3A%2F%2Fwww.autoitscript.com%2Fforum%2Findex.php%3Fact%3Didx&username=myuser&password=mypass&rememberMe=1
HTTP/1.1 200 OK
Date: Wed, 20 Oct 2010 02:20:01 GMT
Server: Apache
X-Powered-By: PHP/5.2.11
Set-Cookie: autoitforumsession_id=***; path=/forum/; domain=.autoitscript.com; httponly
Set-Cookie: autoitforummember_id=***; expires=Thu, 20-Oct-2011 02:20:01 GMT; path=/forum/; domain=.autoitscript.com;
httponly
Set-Cookie: autoitforumpass_hash=***; expires=Wed, 27-Oct-2010 02:20:01 GMT; path=/forum/; domain=.autoitscript.com;
httponly
Set-Cookie: autoitforumcoppa=0; path=/forum/; domain=.autoitscript.com
Set-Cookie: autoitforumsession_id=***; path=/forum/; domain=.autoitscript.com; httponly
Vary: User-Agent,Accept-Encoding
Content-Encoding: gzip
Content-Length: 1219
Keep-Alive: timeout=2, max=1000
Connection: Keep-Alive
Content-Type: text/html;charset=UTF-8
----------------------------------------------------------
http://www.autoitscript.com/forum/index.php?act=idx
GET /forum/index.php?act=idx HTTP/1.1
Host: www.autoitscript.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; es-ES; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10 (.NET CLR
3.5.30729)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: es-es,es;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Referer: http://www.autoitscript.com/forum/index.php?app=core&module=global§ion=login&do=process
Cookie: autoitforumsession_id=***; autoitforumitemMarking_forums_items=***;
autoitforummember_id=***; autoitforumpass_hash=***; autoitforumcoppa=***; autoitforummqtids=***; autoitforumsfc=***;
autoitforumsfct=WinHttp.au3; autoitforumanonlogin=-1
HTTP/1.1 200 OK
Date: Wed, 20 Oct 2010 02:20:04 GMT
Server: Apache
X-Powered-By: PHP/5.2.11
Set-Cookie: autoitforumpass_hash=***; expires=Wed, 27-Oct-2010 02:20:04 GMT; path=/forum/; domain=.autoitscript.com;
httponly
Set-Cookie: autoitforumsession_id=***; path=/forum/; domain=.autoitscript.com; httponly
Vary: User-Agent,Accept-Encoding
Content-Encoding: gzip
Content-Length: 8750
Keep-Alive: timeout=2, max=1000
Connection: Keep-Alive
Content-Type: text/html;charset=UTF-8
Ahora me doy cuenta que está el texto "WinHttp.au3" dentro de los requests. Es raro!
De todos modos, tienen alguna idea de cómo continuar con el código? No intenté _WinHttpSimpleFormFill porque no se puede establecer el referer allí.
Mis dudas en el código son:
1) En _WinHttpOpenRequest no estaba seguro de qué poner en
Código: Seleccionar todo
$ sObjectName [opcional] El nombre del recurso de destino de
el verbo HTTP especificado.
es la dirección URL en la propiedad "action" del form? o la url de el mismo form?
Aquí está el código fuente del formulario de login:
Código: Seleccionar todo
<form id="login" method="post"
action="http://www.autoitscript.com/forum/index.php?app=core&module=global§ion=login&do=process">
<input type="hidden" value="http://www.autoitscript.com/forum/index.php?act=idx" name="referer">
<div id="regular_signin">
<a id="_regularsignin"></a>
<h3 class="bar">Enter your sign in name and password</h3>
<ul>
<li class="field">
<label for="username">Username:</label>
<input type="text" size="25" name="username" class="input_text"
id="username">
</li>
<li class="field">
<label for="password">Password:</label>
<input type="password" size="25" name="password" class="input_text"
id="password"><br>
<a title="Retrieve password" class="desc"
href="http://www.autoitscript.com/forum/index.php?app=core&module=global§ion=lostpass">I've forgotten my
password</a>
</li>
</ul>
</div>
<hr>
<fieldset id="signin_options">
<legend>Sign in options</legend>
<ul>
<li class="field checkbox">
<input type="checkbox" class="input_check" value="1" name="rememberMe"
checked="checked" id="remember">
<label for="remember">
Remember me<br>
<span class="desc">This is not recommended for shared
computers</span>
</label>
</li>
<li class="field checkbox">
<input type="checkbox" class="input_check" value="1"
name="anonymous" id="invisible">
<label for="invisible">
Sign in anonymously<br>
<span class="desc">Don't add me to the active users
list</span>
</label>
</li>
</ul>
</fieldset>
<fieldset class="submit">
<input type="submit" value="Sign In" class="input_submit"> or <a class="cancel"
title="Cancel" href="http://www.autoitscript.com/forum/index.php?">Cancel</a>
</fieldset>
</form>
2) También en esta parte del código del ejemplo:
Código: Seleccionar todo
; Enable cookies for sure
_WinHttpSetOption ($ HRequest, $ WINHTTP_OPTION_DISABLE_FEATURE, $ WINHTTP_DISABLE_COOKIES)
no es en realidad
deshabilitar las cookies lo que está haciendo?
3) Otra cosa que no entiendo por completo es que en un ejemplo de logueo en una cuenta de yahoo, después de ejecutar el script trato de acceder manualmente a mi cuenta en el navegador y no estaba logedado. Sin embargo, el script mostró el body de mi cuenta de correo al final de la ejecución. ¿Cómo puede ser esto?
Disculpen mis preguntas de principiante
y gracias de nuevo por su ayuda!