-
Notifications
You must be signed in to change notification settings - Fork 72
/
class_WinHttpRequest.ahk
114 lines (99 loc) · 2.9 KB
/
class_WinHttpRequest.ahk
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
108
109
110
111
112
113
114
; WinHttpRequest
; Version: 1.00 / 2014-4-3 / tmplinshi
; Tested On: [AHK] 1.1.14.03 A32/U32 | [OS] WinXP SP3
;
; Usage is similar to HTTPRequest (by VxE),
; Please visit the HTTPRequest page (http://goo.gl/CcnNOY) for more details.
;
; Supported Options:
; NO_AUTO_REDIRECT
; Timeout: Seconds
; Proxy: IP:Port
; Codepage: 65001
; Charset: utf-8
; SaveAs: FileName
; Return:
; Success = -1, Timeout = 0, No response = Empty String
;
; How to clear cookie:
; WinHttpRequest( [] )
;
; Thanks:
; VxE for his HTTPRequest (http://goo.gl/CcnNOY)
WinHttpRequest( URL, ByRef In_POST__Out_Data="", ByRef In_Out_HEADERS="", Options="" )
{
static nothing := ComObjError(0)
static oHTTP := ComObjCreate("WinHttp.WinHttpRequest.5.1")
static oADO := ComObjCreate("adodb.stream")
; Clear cookie: WinHttpRequest( [] )
If IsObject(URL) {
oHTTP := ComObjCreate("WinHttp.WinHttpRequest.5.1")
Return
}
; POST or GET
If (In_POST__Out_Data != "") || InStr(Options, "Method: POST")
oHTTP.Open("POST", URL, True)
Else
oHTTP.Open("GET", URL, True)
; HEADERS
If In_Out_HEADERS
{
In_Out_HEADERS := Trim(In_Out_HEADERS, " `t`r`n")
Loop, Parse, In_Out_HEADERS, `n, `r
{
If !( _pos := InStr(A_LoopField, ":") )
Continue
Header_Name := SubStr(A_LoopField, 1, _pos-1)
Header_Value := SubStr(A_LoopField, _pos+1)
If ( Trim(Header_Value) != "" )
oHTTP.SetRequestHeader( Header_Name, Header_Value )
}
}
If (In_POST__Out_Data != "")
oHTTP.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
; Options
If Options
{
Loop, Parse, Options, `n, `r
{
If ( _pos := InStr(A_LoopField, "Timeout:") )
Timeout := SubStr(A_LoopField, _pos+8)
Else If ( _pos := InStr(A_LoopField, "Proxy:") )
oHTTP.SetProxy( 2, SubStr(A_LoopField, _pos+6) )
Else If ( _pos := InStr(A_LoopField, "Codepage:") )
oHTTP.Option(2) := SubStr(A_LoopField, _pos+9)
}
oHTTP.Option(6) := InStr(Options, "NO_AUTO_REDIRECT") ? 0 : 1
}
; Send...
oHTTP.Send(In_POST__Out_Data)
ReturnCode := oHTTP.WaitForResponse(Timeout ? Timeout : -1)
; Handle "SaveAs:" and "Charset:"
If InStr(Options, "SaveAs:")
{
RegExMatch(Options, "i)SaveAs:[ \t]*\K[^\r\n]+", SavePath)
oADO.Type := 1
oADO.Open()
oADO.Write( oHTTP.ResponseBody )
oADO.SaveToFile( SavePath, 2 )
oADO.Close()
In_POST__Out_Data := ""
}
Else If InStr(Options, "Charset:")
{
RegExMatch(Options, "i)Charset:[ \t]*\K\w+", Encoding)
oADO.Type := 1
oADO.Mode := 3
oADO.Open()
oADO.Write( oHTTP.ResponseBody() )
oADO.Position := 0
oADO.Type := 2
oADO.Charset := Encoding
In_POST__Out_Data := IsByRef(In_POST__Out_Data) ? oADO.ReadText() : ""
}
Else
In_POST__Out_Data := IsByRef(In_POST__Out_Data) ? oHTTP.ResponseText : ""
; output headers
In_Out_HEADERS := "HTTP/1.1 " oHTTP.Status() "`n" oHTTP.GetAllResponseHeaders()
Return, ReturnCode ; Success = -1, Timeout = 0, No response = Empty String
}