-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHTTP.au3
107 lines (90 loc) · 2.63 KB
/
HTTP.au3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_AU3Check_Parameters=-d
#AutoIt3Wrapper_Run_Tidy=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
AutoItSetOption("TCPTimeout", 1000)
TCPStartup()
Global $HttpSocket = -1
Func HttpConnect($sIpAddress)
$HttpSocket = TCPConnect($sIpAddress, 80)
If ($HttpSocket == -1) Then
ConsoleWrite("HTTP Connect Error" & @CRLF)
Exit
EndIf
EndFunc ;==>HttpConnect
Func HttpClose()
TCPCloseSocket($HttpSocket)
$HttpSocket = -1
EndFunc ;==>HttpClose
Func HttpGet($sIpAddress, $sUri, ByRef $aHeaders)
Dim $sRequest
If StringLeft($sUri, 1) <> "/" Then $sUri = "/" & $sUri
$sRequest = "GET " & $sUri & " HTTP/1.1" & @CRLF
$sRequest &= "Host: " & $sIpAddress & @CRLF
If UBound($aHeaders) > 1 Then
$sRequest &= $aHeaders[0] & @CRLF
$sRequest &= $aHeaders[1] & @CRLF
EndIf
$sRequest &= "Connection: close" & @CRLF
$sRequest &= "" & @CRLF
If $iDebugLevel > 1 Then
ConsoleWrite("# HTTP Request:" & @CRLF)
ConsoleWrite($sRequest)
EndIf
Local $bytes = TCPSend($HttpSocket, $sRequest)
If $bytes == 0 Then
ConsoleWrite("HTTP GET Error" & @CRLF)
Exit
EndIf
EndFunc ;==>HttpGet
Func HttpPost($sIpAddress, $sUri, ByRef $aHeaders, $sXml)
Dim $sRequest
Dim $datasize = StringLen($sXml)
If StringLeft($sUri, 1) <> "/" Then $sUri = "/" & $sUri
$sRequest = "POST " & $sUri & " HTTP/1.1" & @CRLF
$sRequest &= "Host: " & $sIpAddress & @CRLF
If UBound($aHeaders) > 1 Then
$sRequest &= $aHeaders[0] & @CRLF
$sRequest &= $aHeaders[1] & @CRLF
EndIf
$sRequest &= "Connection: close" & @CRLF
$sRequest &= "Content-Type: application/x-www-form-urlencoded" & @CRLF
$sRequest &= "Content-Length: " & $datasize & @CRLF
$sRequest &= "" & @CRLF
$sRequest &= $sXml & @CRLF
If $iDebugLevel > 1 Then
ConsoleWrite("# HTTP Request:" & @CRLF)
ConsoleWrite($sRequest)
ConsoleWrite(@CRLF)
EndIf
Local $bytes = TCPSend($HttpSocket, $sRequest)
If $bytes == 0 Then
ConsoleWrite("HTTP POST Error" & @CRLF)
Exit
EndIf
EndFunc ;==>HttpPost
Func HttpRead()
Local $body = ""
Local $data = ""
While 1
Local $bytes = TCPRecv($HttpSocket, 500)
If @error Then
ConsoleWrite("HTTP Error:" & @error)
Exit
EndIf
$data &= $bytes
If StringLen($bytes) == 0 Then ExitLoop
Sleep(100)
WEnd
If $iDebugLevel > 1 Then
ConsoleWrite("# HTTP Response:" & @CRLF)
ConsoleWrite($data & @CRLF)
EndIf
$body = StringMid($data, StringInStr($data, @CRLF & @CRLF) + 2)
;ConsoleWrite(@CRLF & "[" & $body & "]" & @CRLF & @CRLF)
If StringLen(StringStripWS($body, $STR_STRIPALL)) = 0 Then
ConsoleWrite("Error: No body found")
Exit
EndIf
Return $body
EndFunc ;==>HttpRead