Ce soir j'ai adapté mes nouvelles connaissances pour essayer de vous montrer ce qui se cache derrière InetGet. La fonction que je vous presente marche presque de la meme manière qu'InetGet à quelques adaptions près, et vous pourrez ainsi voir comment on peut utiliser le TCP/IP afin de faire de multiples actions (Comme Remplir un formulaire, Explications sur demande) sans utiliser Firefox ou IE.
(Attention, La fonction InetGet est peut etre totalement differente, je ne connais pas son code source,mais en tout cas je presume qu'elle doit s'en rapprocher fortement.)
Cette fonction pourra egalement donner des idées pour ceux qui ont besoin d'informations concernant le TCP/IP avec Autoit.
Je precise que cette fonction marche independament de IE ou Firefox
Voila un exemple d'utilisation:
Code : Tout sélectionner
$adresse = "autoitscript.fr"
$file = "/forum/" ; Ou
$response = _InetGet2($adresse,$file)
;MsgBox(0,"",@error&@crlf&@extended)
$hFile = FileOpen(@ScriptDir & "\result.HTML",2)
FileWrite($hFile,$response)
ShellExecute(@ScriptDir & "\result.HTML")
► Afficher le textefonction
Code : Tout sélectionner
Func _InetGet2($host,$file,$useragent = "Macro",$flag =0)
If String($host) <> $host Then Return -1
If String($file) <> $file Then Return -1
If String($useragent) <> $useragent Then Return -1
$_HTTPUserAgent = String("Autoit")&$useragent
TCPStartup()
$ip = TCPNameToIP ( $host )
$socket = TCPConnect ( $ip, 80 )
If ($socket == -1) Then
SetError(1, @error)
Return 0
EndIf
$command = "GET "&$file&" HTTP/1.1"&@CRLF
$command &= "Host: " &$host&@CRLF
$command &= "User-Agent: "&$_HTTPUserAgent&@CRLF
$command &= "Connection: close"&@CRLF
$command &= ""&@CRLF
$bytessent = TCPSend($socket, $command)
If $bytessent == 0 Then
SetError(2,@error)
return 0
EndIf
Dim $timer = TimerInit()
Dim $performancetimer = TimerInit()
Dim $downloadtime = 0
Dim $_HTTPRecvTimeout = 20*1000 ; = 20 Secondes
Dim $headers[1][2] ; An Array of the headers found
Dim $numheaders = 0 ; The number of headers found
Dim $body = "" ; The body of the message
Dim $HTTPVersion ; The HTTP version of the server (almost always 1.1)
Dim $HTTPResponseCode ; The HTTP response code like 200, or 404
Dim $HTTPResponseReason ; The human-readable response reason, like "OK" or "Not Found"
Dim $bytesreceived = 0 ; The total number of bytes received
Dim $data = "" ; The entire raw message gets put in here.
Dim $chunked = 0 ; Set to 1 if we get the "Transfer-Encoding: chunked" header.
Dim $chunksize = 0 ; The size of the current chunk we are processing.
Dim $chunkprocessed = 0 ; The amount of data we have processed on the current chunk.
Dim $contentlength ; The size of the body, if NOT using chunked transfer mode.
Dim $part = 0 ; Refers to what part of the data we're currently parsing:
; 0 - Nothing parsed, so HTTP response should come next
; 1 - Currently parsing headers
; 2 - Currently waiting for the next chunk size - this is skipped if the transfer-encoding is not chunked
; 3 - Currently waiting for or parsing body data
; 4 - Currently parsing footers
While 1
Sleep(10)
Dim $recv = TCPRecv($socket,4096)
If $recv <> "" Then
$bytesreceived = $bytesreceived + StringLen($recv)
$timer = TimerInit()
$data &= $recv
ConsoleWrite("Bytes downloaded: "&$bytesreceived&@CRLF)
EndIf
Dim $split = StringSplit($data,@CRLF,1)
$data = ""
Dim $i
For $i=1 To $split[0]
If $i=$split[0] Then
If $part < 2 OR $chunked = 1 Then
; This is tricky. The last line we've received might be truncated, so we only want to process it under special cases.
; Non chunked data doesn't always send a CRLF at the end so there's no way to tell if this is truly the last line without parsing it.
; However, we don't want to parse it if it's only a partial header or something.
; The solution: We will only process this last line if we're at the body section and the transfer-encoding is NOT chunked.
$data = $split[$i]
ExitLoop
EndIf
EndIf
Dim $newpart = $part
Switch $part
Case 0 ; Nothing parsed, so HTTP response should come next
If $split[$i] <> "" Then
Dim $regex = StringRegExp($split[$i],"^HTTP/([0-9.]+) ([0-9]+) ([a-zA-Z0-9 ]+)$",3)
If @error <> 0 Then
SetError(5)
Return $split[$i]
Else
$HTTPVersion = $regex[0]
$HTTPResponseCode = $regex[1]
$HTTPResponseReason = $regex[2]
If $HTTPResponseCode <> 100 Then
$newpart = 1
EndIf
EndIf
EndIf
Case 1, 4 ; Currently parsing headers or footers
;If the line is blank, then we're done with headers and the body is next
If $split[$i] == "" Then
If $part = 1 Then
If $chunked Then
$newpart = 2
Else
$newpart = 3
EndIf
ElseIf $part = 4 Then
; If $part is 4 then we're processing footers, so we're all done now.
ExitLoop 2
EndIf
Else ;The line wasn't blank
;Check to see if the line begins with whitespace. If it does, it's actually
;a continuation of the previous header
Dim $regex = StringRegExp($split[$i], "^[ \t]+([^ \t].*)$", 3)
If @error <> 1 Then
If $numheaders == 0 Then
SetError(6)
Return $split[$i]
EndIf
$headers[$numheaders-1][1] &= $regex[0]
Else;The line didn't start with a space
Dim $regex = StringRegExp($split[$i],"^([^ :]+):[ \t]*(.*)$",3)
If @error <> 1 Then
;This is a new header, so add it to the array
$numheaders = $numheaders + 1
ReDim $headers[$numheaders][2]
$headers[$numheaders-1][0] = $regex[0]
$headers[$numheaders-1][1] = $regex[1]
; There are a couple headers we need to know about. We'll process them here.
If $regex[0] = "Transfer-Encoding" AND $regex[1] = "chunked" Then
$chunked = 1
ElseIf $regex[0] = "Content-Length" Then
$contentlength = Int($regex[1])
EndIf
Else
SetError(6)
Return $split[$i]
EndIf
EndIf
EndIf
Case 2 ; Awaiting chunk size
$regex = StringRegExp($split[$i],"^([0-9a-f]+);?.*$",3)
If @error <> 0 Then
SetError(8)
Return $split[$i]
EndIf
$chunksize = $regex[0]
$chunksize = Dec($chunksize)
$chunkprocessed = 0
If $chunksize == 0 Then
$newpart = 4
Else
$newpart = 3
EndIf
Case 3 ; Awaiting body data
$body &= $split[$i]
$chunkprocessed = $chunkprocessed + StringLen($split[$i])
If $chunked Then
If $chunkprocessed >= $chunksize Then
$newpart = 2
Else
$body &= @CRLF
$chunkprocessed = $chunkprocessed + 2; We add 2 for the CRLF we stipped off.
EndIf
Else
If $chunkprocessed >= $contentlength Then
ExitLoop 2
Else
If $i < $split[0] Then
; Only add a CRLF if this is not the last line received.
$body &= @CRLF
$chunkprocessed = $chunkprocessed + 2; We add 2 for the CRLF we stipped off.
EndIf
EndIf
EndIf
Case Else
; This should never happen
EndSwitch
$part = $newpart
Next
If $bytesreceived == 0 AND TimerDiff($timer) > $_HTTPRecvTimeout Then
SetError(3)
Return 0
ElseIf $bytesreceived > 0 AND TimerDiff($timer) > $_HTTPRecvTimeout Then
ConsoleWrite($body)
SetError(4)
Return $bytesreceived
EndIf
WEnd
$downloadtime = TimerDiff($performancetimer)
Switch $flag
Case 0
SetError(0)
TCPCloseSocket($socket)
TCPShutdown ( )
Return $body
Case 1
Dim $return[5]
$return[0] = $HTTPResponseCode
$return[1] = $HTTPResponseReason
$return[2] = $HTTPVersion
$return[3] = $headers
$return[4] = $body
SetError(0)
TCPCloseSocket($socket)
TCPShutdown ( )
Return $return
Case Else
SetError(7)
TCPCloseSocket($socket)
TCPShutdown ( )
Return 0
EndSwitch
EndFunc
A +,
Tim