-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWinHttpRequest.ahk
138 lines (115 loc) · 3.72 KB
/
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
r := WinHttpRequest("http://www.tunwalai.com/chapter/2514735/%e0%b8%95%e0%b8%ad%e0%b8%99%e0%b8%97%e0%b8%b5%e0%b9%88-3", InOutData := "", InOutHeaders := Headers(), "Timeout: 1")
MsgBox, % (r = -1) ? "successful" : (r = 0) ? "Timeout" : "No response"
MsgBox, % InOutData
Clipboard := InOutData
MsgBox, % InOutHeaders
Return
Headers(referer = "")
{
Headers =
( LTrim
Referer: %referer%
User-Agent: Mozilla/5.0 (Windows NT 10.0`; Win64`; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36
)
Return Headers
}
; WinHttpRequest.ahk
;
; 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: <CPnnn> - e.g. "Codepage: 65001"
; Charset: <Encoding> - e.g. "Charset: UTF-8"
; SaveAs: <FileName>
; Return:
; Success = -1, Timeout = 0, No response = Empty String
;
; How to clear cookie:
; WinHttpRequest( [] )
;
; ChangeLog:
; 2015-4-25 - Added option "Method: HEAD"
; 2014-9-7 - Fixed a bug in "Charset:"
; 2014-7-11 - Fixed a bug in "Charset:"
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
If IsObject(URL)
Return oHTTP := ComObjCreate("WinHttp.WinHttpRequest.5.1")
; POST or GET
If (In_POST__Out_Data != "") || InStr(Options, "Method: POST")
oHTTP.Open("POST", URL, True)
Else If InStr(Options, "Method: HEAD")
oHTTP.Open("HEAD", 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 != "") && !InStr(In_Out_HEADERS, "Content-Type:")
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() : ""
oADO.Close()
}
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
}